Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey: Inject Spring component into ContainerRequestFilter

I am using Jersey 1.4 ea together with Spring 3.0 and the jersey-spring integration. Integrating Jersey and Spring works fine for resource classes as described here. How ever I want to inject a spring component into a ContainerRequestFilter to do some pre-processing of requests.

@Component
public class SecurityFilter implements ContainerRequestFilter {

    // UserManager is a declared spring component
    // Injecting it should work somehow
    @Autowired
    private UserManager userManager;

    @Override
    public ContainerRequest filter(ContainerRequest request) {
        System.out.println(userManager);
        // prints out null on request
    }
}

Both the filter and the user manager bean are registered when I deploy the application to Glassfish. I wonder what am I doing wrong. Is there a way to inject a spring managed bean into a ContainerRequestFilter?

UPDATE

Kind of solved. The issue is that Jersey does not obtain Spring beans if these beans are Java proxies (opposed to generated proxy classes). The problem can be solved by instructing Spring to ALWAYS use proxy classes instead of Java Proxies by specifying the proxy-target-class="true" attribute in the respective parts of a spring configuration. In my scenario I had to specify it on a <tx:annotation-driven proxy-target-class="true" />.

See here for a more detailed analysis and a possible fix on that.

like image 334
nre Avatar asked Sep 19 '10 23:09

nre


1 Answers

I'm seeing the same thing with Jersey 1.6 and Spring 3.0.5. Using the debugger, I can tell that even though my code is marked with @Component, both Spring and Jersey will instantiate their own copy of of this class:

@Path("/beams")
@Produces("text/xml")
@Component
@Scope("singleton")
public class BeamsResource {
}

There's some chatter that this will be added in a future version of Jersey, but it doesn't seem to be working now. I know this is a hideous solution, but I'm using a static member variable to hook together Jersey and Spring for the time being. Bleh.

like image 182
Don Branson Avatar answered Oct 06 '22 15:10

Don Branson