如何将Jersey 2与Spring IoC容器一起使用
向Jersey 2中注入spring Bean的最佳方法是什么?泽西似乎不支持这一点如何将Jersey 2与Spring IoC容器一起使用,spring,jersey,Spring,Jersey,向Jersey 2中注入spring Bean的最佳方法是什么?泽西似乎不支持这一点 将这两个框架连接在一起需要什么?在pom.xml和web.xml中?您应该能够对jersey组件进行注释,然后使用注释注入bean @Service //(or @Component) public class MyJerseyService { @Autowired private MyObj mySpringBean } 截至2013年6月,Jersey 2.0没有官方的Spring支
将这两个框架连接在一起需要什么?在pom.xml和web.xml中?您应该能够对jersey组件进行注释,然后使用注释注入bean
@Service //(or @Component)
public class MyJerseyService {
@Autowired
private MyObj mySpringBean
}
截至2013年6月,Jersey 2.0没有官方的Spring支持。有两种选择:
编辑:Jersey 2.3现在具有spring支持,请参见下面Fabio的答案Jersey 2.3现在具有spring支持: 如文件中所述 Spring extension模块配置基于注释 因此,您必须告诉spring扫描您的类路径,例如:
<context:component-scan base-package="my.package.to.resources">
谢谢你的回答!由于控制器不是通过spring访问的,而是通过jax-rsservlet访问的,所以需要什么将它们连接在一起呢?spring需要了解它才能注入bean。google搜索spring组件扫描应该有帮助,spring如何能够将依赖项注入到一个不是spring bean的类中?当您使用
@Service
或@component
对它进行注释时,它就变成了spring bean(在其他注释中)而且在您的上下文配置中,您放置的
Jersey 2.0将无法正确管理Spring组件的生命周期。它使用的HK2DI框架还不支持Spring。为了让它工作,泽西岛的注入机制必须让Spring意识到。现在是2017年,我没有看到泽西岛2.3(),我没有得到什么?@sschras 2.3<2.26,因为3<26:)
@Component
@Singleton
@Path("example")
public class Example {
//Spring beans can't be injected directly into JAX-RS classes by using Spring XML configuration
@Autowired
private MyOtherBean myOtherBean;
@GET @Path("hello")
public String hello() {
return myOtherBean.hello();
}
}