Jersey:将 Spring 组件注入 ContainerRequestFilter

发布于 2024-09-24 06:05:23 字数 1423 浏览 0 评论 0原文

我将 Jersey 1.4 ea 与 Spring 3.0 和 jersey-spring 集成一起使用。对于资源类,集成 Jersey 和 Spring 效果很好,如此处所述。 我多么想将 spring 组件注入 ContainerRequestFilter 对请求进行一些预处理。

@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
    }
}

当我将应用程序部署到 Glassfish 时,过滤器和用户管理器 bean 都会注册。我想知道我做错了什么。有没有办法将 spring 托管 bean 注入 ContainerRequestFilter 中?

更新

已经解决了。问题是,如果 Spring bean 是 Java 代理(与生成的代理类相反),Jersey 不会获取这些 Spring bean。通过在 Spring 配置的各个部分指定 proxy-target-class="true" 属性,指示 Spring 始终使用代理类而不是 Java 代理,可以解决此问题。在我的场景中,我必须在 上指定它。

请参阅此处了解更详细的分析和可能的修复。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-10-01 06:05:23

我在 Jersey 1.6 和 Spring 3.0.5 中看到了同样的情况。使用调试器,我可以看出,即使我的代码标有 @Component,Spring 和 Jersey 都会实例化它们自己的此类副本:

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

有一些传言称这将在 Jersey 的未来版本中添加,但它并没有现在好像不工作了我知道这是一个可怕的解决方案,但我暂时使用静态成员变量将 Jersey 和 Spring 连接在一起。布莱赫。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文