javaspringfacescontext

Howto get @Service instance in Spring programatically


I need to implement javax.faces.convert.Converter to convert String-to-Object and Object-to-String.

To do so, I have defined specific services (@Service), but I do not know how to get an instance.

I have tried to use @Autowired and @Component to get instance, but Spring is ignoring.

Is it possible to get @Service instance from FacesContext?


Solution

  • It's impossible. Spring annotations are useless if it didn't configure to use them in your applications.

    First You should get the application context like this

    ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
    

    Then use this context to get instance of the component.

    YourService custB = (YourService )ctx.getBean("yourService");