简体   繁体   English

将Spring bean注入RestEasy

[英]Inject Spring beans into RestEasy

Is it possible to inject Spring beans into an RestEasy @Path class? 是否可以将Spring bean注入RestEasy @Path类? 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). 我设法使用@InjectParam注释使用Jersey,但由于其他一些原因,我需要切换到RestEasy,我似乎无法找到一种方法(尝试好的''javax.inject.Inject,但没什么。

EDIT 编辑

This solution works: http://www.mkyong.com/webservices/jax-rs/resteasy-spring-integration-example/ 此解决方案有效: 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. 但它不是注射..我仍然喜欢更优雅的东西。

Simply annotate your RestEasy class with Spring's @Component and then inject your beans using Spring's @Autowired. 使用Spring的@Component简单地注释你的RestEasy类,然后使用Spring的@Autowired注入你的bean。 Don't forget to include the annotation-config and component-scan elements in your spring configuration. 不要忘记在spring配置中包含annotation-config和component-scan元素。

有一个工作示例将RestEasy与Spring集成,只需尝试spring-resteasy

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 . 我完全同意Peter的回答,但还有另一种方法可以做到这一点:你制作扩展SpringBeanAutowiringSupport所有展示bean(RESTEasy或JAX-WS,它们不是Spring组件)。

That way you can easily inject your Spring Services by @Autowired annotation in these classes. 这样,您可以通过@Autowired注释轻松地在这些类中注入Spring Services。

You could use the @Configurable annotation to make a normal class (created by new ) a spring Bean. 您可以使用@Configurable批注将普通类(由new创建)作为spring Bean。 Then you can use the normal Spring annotation to inject everything in that class/instance like in a "normal" Spring Bean. 然后你可以使用普通的Spring注释来注入该类/实例中的所有内容,就像在“普通”Spring Bean中一样。

But that requires AspectJ! 但这需要AspectJ!

@See Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring @See Spring Reference Chapter 7.8.1使用AspectJ依赖Spring注入域对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM