users@jersey.java.net

[Jersey] Jersey's limit with Spring injection

From: Michael-O <1983-01-06_at_gmx.net>
Date: Wed, 16 Apr 2014 19:21:29 +0200

Hi,

I am examining Jersey's Spring module and is has a limitation as stated
in the user guide:

"Spring beans can't be injected directly into JAX-RS classes by using
Spring XML configuration"

I do not fully understand that and try to adapt that to my usecase. We
have Spring 4.0.x, Jersey 2.7 in evaluation right now.

I have modified the SpringComponentProvider because I was not satisfied
with the way it interacts with Spring. The approach is now the same as
in the Spring DispatcherServlet:

1. The Spring ContextListener creates a root context
2. The SpringComponentProvider retrieves this context and creates a
child context on top of that

The root context has some common beans like services, which require
repositories and other stuff. The child context decares the resources beans-

All dependencies are annotated with @Autowired, context component scan
is not used but annotation config.

Now my resource looks like this:

// Located in the JAX-RS context, parent: root context
@Component
@Path(...)
public Resource {

   @Autowired
   MyService service;

   @GET
   public String hello() {
     return service.hello();
   }

}

and the service:

// Located in the root context
public DefaultMyService implements MyService {

   @Autowired
   MyRepository repository;

   public String hello() {
     ...
     return repository.hello();
   }

}

As you can see, I have bean dependencies over two hierarchy levels.

This approach works very well with the SpringWSServlet and JAX-WS Metro.
When I deploy the same setup with Jersey, service remains null and is
not wired. The AutowiredInjectResolver does not find the matching bean.
The weird issue is that,

1. if I swap @Autowired for @Inject, everything is fine,
2. if I add "@Autowired Properties config" to Resource where the former
is located in the root context, the wiring works perfectly.

The questions are:

1. Is that the limitation mention in the user guide?
2. If not, who is cauesing that issue, the HK2 Spring Bridge, unable to
handle context and/or bean hierarchies of the Jersey Spring module?
3. Why does @Inject work? More over, who is injecting, Spring or HK2?

Help is very much appreciated,

Michael