如何在Spring IoC容器中使用Jersey 2

use*_*836 13 spring jersey

将春豆注入泽西2的最佳方法是什么?泽西岛似乎不支持这种本土化.

将2个框架连接在一起需要什么?在pom.xml和web.xml中?

Fab*_*nte 15

泽西岛2.3现在有弹簧支持:

https://jersey.github.io/documentation/latest/user-guide.html#spring

如文档中所述

Spring扩展模块配置基于注释

所以你必须告诉spring扫描你的类路径,例如:

<context:component-scan base-package="my.package.to.resources">
Run Code Online (Sandbox Code Playgroud)

并使用spring注释注释您的资源类(我建议使用@Component,然后指定jersey资源范围@ Singleton/@ PerLookup/@RequestScoped)

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)


ant*_*tix 11

截至2013年6月,Jersey 2.0没有正式的Spring支持.有两种选择:

  1. 使用第三方代码https://github.com/marko-asplund/jersey/tree/master/ext/jersey-spring
  2. 等到HK2 spring bridge变得稳定并记录在https://java.net/jira/browse/HK2-40

也可以看看:

http://jersey.576304.n2.nabble.com/Spring-framework-support-for-Jersey-2-td7580673.html

编辑:泽西岛2.3现在有弹簧支持,请参阅Fabio下面的答案


Jef*_*rey -7

您应该能够对球衣组件进行注释,然后使用注释来注入 Bean。

@Service //(or @Component)
public class MyJerseyService {

    @Autowired
    private MyObj mySpringBean

}
Run Code Online (Sandbox Code Playgroud)

  • Jersey 2.0 不会正确管理 Spring 组件的生命周期。它使用 HK2 DI 框架,尚不支持 Spring。为了使其工作,Jersey 中的注入机制必须让 Spring 感知。 (3认同)