开发者

Jersey: Inject Spring component into ContainerRequestFilter

开发者 https://www.devze.com 2023-01-16 15:19 出处:网络
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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消