13

What is the best way to enable injection of spring beans into Jersey 2? Jersey seems to not support this natively.

What is needed to wire the 2 frameworks together? In pom.xml and web.xml?

user1829836
  • 143
  • 1
  • 1
  • 6

3 Answers3

15

Jersey 2.3 has now spring support:

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

As stated in the documentation

The Spring extension module configuration is based on annotations

So you have to tell spring to scan your classpath, for example:

<context:component-scan base-package="my.package.to.resources">

and annotate your resource class with a spring annotation (I advise to use @Component, and then specify the jersey resource scopes @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();
    }
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Fabio Bonfante
  • 5,128
  • 1
  • 32
  • 37
11

As of June 2013, Jersey 2.0 has no official Spring support. There are two options:

  1. Use third party code from here https://github.com/marko-asplund/jersey/tree/master/ext/jersey-spring
  2. Wait until HK2 spring bridge becomes stable and documented https://java.net/jira/browse/HK2-40

See also:

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

EDIT: Jersey 2.3 has spring support now, see the answer by Fabio below

anttix
  • 7,709
  • 1
  • 24
  • 25
-7

You should be able to annotate jersey components and then use annotations to inject the beans.

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

    @Autowired
    private MyObj mySpringBean

}
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • Thanks for your answer! What is needed to wire them together, since the controller is not accessed via spring, but with jax rs servlet? – user1829836 Jan 31 '13 at 14:50
  • spring needs to know about it to inject beans. a google search for spring component scanning should help – Jeff Storey Jan 31 '13 at 15:15
  • How is spring able to inject dependencies into a class that is not a spring bean? – user1829836 Jan 31 '13 at 15:28
  • It becomes a spring bean when you annotate it with `@Service` or `@Component` (among other annotations) and in your context configuration you put `` – Jeff Storey Jan 31 '13 at 15:40
  • 3
    Jersey 2.0 will not manage the lifecycle of Spring components properly. It uses HK2 DI framework that does not support Spring yet. To make it work, the injection mechanism in Jersey has to be made Spring aware. – anttix Jul 10 '13 at 17:55
  • You are correct. I didn't realize it didn't work properly in Jersey 2. I use com.sun.jersey.spi.spring.container.servlet.SpringServlet as the jersey servlet in jersey 1.x – Jeff Storey Jul 10 '13 at 18:45