Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 849
Valijon Avatar asked May 30 '26 16:05

Valijon


1 Answers

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");
like image 94
Roman C Avatar answered Jun 02 '26 07:06

Roman C