将 Spring beans 注入 RestEasy
是否可以将 Spring bean 注入 RestEasy @Path 类?我设法用 Jersey 和 @InjectParam 注释来做到这一点,但由于其他一些原因,我需要切换到 RestEasy,而且我似乎找不到一种方法来做到这一点(尝试了很好的 ol' javax.inject.Inject,但什么也没有)。
编辑
此解决方案有效: http://www.mkyong.com/webservices/jax-rs/resteasy-spring -integration-example/
但它不是注入..我仍然更喜欢更优雅的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用 Spring 的 @Component 注释您的 RestEasy 类,然后使用 Spring 的 @Autowired 注入您的 bean。不要忘记在 spring 配置中包含注释配置和组件扫描元素。
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.
有一个将 RestEasy 与 Spring 集成的工作示例,只需尝试 spring-resteasy 即可。
There is a working example that integrates RestEasy with Spring just try spring-resteasy.
您可以使用
@Configurable
注释将普通类(由new
创建)创建为Spring Bean。然后,您可以使用普通的 Spring 注释来注入该类/实例中的所有内容,就像在“普通”Spring Bean 中一样。
但这需要 AspectJ!
@参见Spring参考章节7.8.1 使用AspectJ通过Spring依赖注入域对象
You could use the
@Configurable
annotation to make a normal class (created bynew
) 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
我完全同意 Peter 的回答,但还有另一种方法:让所有的解释 bean(RESTEasy 或 JAX-WS,它们不是 Spring 组件)扩展 SpringBeanAutowiringSupport。
这样你就可以通过@Autowired注释轻松地在这些类中注入你的Spring服务。
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.