我可以在拦截器上声明生命周期拦截器吗?

发布于 2024-08-27 10:53:54 字数 499 浏览 4 评论 0原文

我的业务 bean 是这样定义的:

@Local
@Interceptors(BusinessInterceptor.class})
public class MyBean implements SomeBean { ... }

然后我希望使用 Spring 的 SpringBeanAutowiringInterceptor 配置我的 BusinessInterceptor

@Interceptors(SpringBeanAutowiringInterceptor.class)
public class BusinessInterceptor {
    @Autowired
    private SomeSpringBean someSpringBean;
}

这是允许/合法的吗?我收到错误(主要是 NPE),表明 BusinessInterceptor 中的字段尚未正确初始化。

I have my business bean defined thus:

@Local
@Interceptors(BusinessInterceptor.class})
public class MyBean implements SomeBean { ... }

And then I want my BusinessInterceptor to be configured with Spring's SpringBeanAutowiringInterceptor:

@Interceptors(SpringBeanAutowiringInterceptor.class)
public class BusinessInterceptor {
    @Autowired
    private SomeSpringBean someSpringBean;
}

Is this allowed/legal? I'm getting errors (NPEs, mostly) suggesting that the fields in BusinessInterceptor have not been initialized properly.

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

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

发布评论

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

评论(1

缘字诀 2024-09-03 10:53:54

我怀疑这是否可行。如果我很好地理解你的场景,你基本上有两个 DI 容器,一个是 Spring,另一个是应用程序。服务器本身。每个管理不同的元素。 BusinessInterceptor 由应用创建。服务器不知道Spring——那么就不会设置@Autowired bean。

(请注意,Spring 和 EJB3 现在已经变得非常相似。您可以使用 Spring 获得与 EJB 相同的功能。Spring 确实具有类似于 EJB3 拦截器的声明式事务、依赖项注入和 AOP 功能(这些是主要的托管功能)。另一方面另一方面,EJB3 现在是如此轻量级,以至于没有真正令人信服的理由将 Spring 与 EJB3 一起使用,请参见 企业 Java 的未来:全栈 Spring 或全栈 Java EE 但这并不能回答问题,只是我的一点题外话:)

I doubt this can work. If I understand well your scenario, you have basically two DI containers, one is Spring and the other the app. server itself. Each one manages different elements. The BusinessInterceptor is created by the app. server which is unaware of Spring -- the @Autowired bean is then not set.

( Note that Spring and EJB3 have become quite similar now. You can have the same functionalities as EJB with Spring. Spring has indeed declarative transactions, dependency injection and AOP facilities similar to EJB3 interceptors (these are the main managed features). On the other hand, EJB3 is now so lightweight that there isn't really a compelling reason to use Spring with with EJB3. See Future of enterprise Java: full stack Spring or full stack Java EE. But this does not answer the question, and is just a little digression of mine :)

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