我可以在拦截器上声明生命周期拦截器吗?
我的业务 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这是否可行。如果我很好地理解你的场景,你基本上有两个 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 :)