Inject Spring beans into RestEasy

﹥>﹥吖頭↗ 提交于 2019-11-30 03:58:06

问题


Is it possible to inject Spring beans into an RestEasy @Path class? I managed to do it with Jersey, with @InjectParam annotation, but for some other reasons, I need to switch to RestEasy, and I can't seem to find a way to do it (tried good ol' javax.inject.Inject, but nothing).

EDIT

This solution works: http://www.mkyong.com/webservices/jax-rs/resteasy-spring-integration-example/

but it's not injection.. I'd still prefer something a little more elegant.


回答1:


Simply annotate your RestEasy class with Spring's @Component and then inject your beans using Spring's @Autowired. Don't forget to include the annotation-config and component-scan elements in your spring configuration.




回答2:


There is a working example that integrates RestEasy with Spring just try spring-resteasy.




回答3:


You could use the @Configurable annotation to make a normal class (created by new) a spring Bean. Then you can use the normal Spring annotation to inject everything in that class/instance like in a "normal" Spring Bean.

But that requires AspectJ!

@See Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring




回答4:


I totally agree with Peter's answer but there is another way to do it: you make all your exposition beans (RESTEasy or JAX-WS, which are not Spring components) extending the SpringBeanAutowiringSupport.

That way you can easily inject your Spring Services by @Autowired annotation in these classes.



来源:https://stackoverflow.com/questions/9009660/inject-spring-beans-into-resteasy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!