是否可以将@RequestScoped bean @Inject 到@Stateless EJB 中? [英] Is it possible to @Inject a @RequestScoped bean into a @Stateless EJB?
问题描述
是否可以将请求范围的 CDI bean 注入无状态会话 bean 中?
Is it possible to inject a request-scoped CDI bean into a Stateless session bean?
我问了一个相关的问题,并认为具体的 CDI @RequestScoped 到 @Stateless 问题值得自己发表.
I had asked a related question and thought the specific CDI @RequestScoped into @Stateless question merited its own post.
在 EJB 方法/@RequestScoped 和 @Stateless 之间传递状态一个>
我也问了一个关于 JMS @MessageDriven beans 的类似问题 - 基本上想知道关于 @Stateless 的相同问题.
I also asked a similar question about JMS @MessageDriven beans - basically want to know the same about @Stateless.
@RequestScoped CDI 注入@MessageDriven bean
推荐答案
你绝对可以做你提到的事情并在 @Stateless
会话 bean 中使用 @RequestScoped
bean 和@MessageDriven
bean.这是 CDI 规范和 TCK 的核心部分,保证可移植.
You can absolutely do what you mention and use @RequestScoped
beans in an @Stateless
session bean and an @MessageDriven
bean. This is a core part of the CDI spec and TCK and guaranteed portable.
请注意,有一个使用 @RequestScoped
bean 的 @Stateless
bean 的测试,但没有保证 @MessageDriven 的测试
bean 可以引用 @RequestScoped
bean.这只是一个疏忽,已经针对 Java EE 7 TCK 修复.所以请注意,如果它不适用于 MDB 案例,那可能不是你的错:)
Do be aware that there is a test for a @Stateless
bean that uses a @RequestScoped
bean, but there is no test that guarantees a @MessageDriven
bean can reference @RequestScoped
beans. This was just an oversight and is already fixed for the Java EE 7 TCK. So just be aware that if it doesn't work for the MDB case, it may not be your fault :)
解决方法是简单地将 MDB 委托给任何类型的 SessionBean,如 @Stateless
、@Stateful
和 @Singleton
都有 @RequestScoped
测试.
The workaround would be to simply have your MDB delegate to a SessionBean of any kind as @Stateless
, @Stateful
, and @Singleton
all have @RequestScoped
tests.
虽然 @Stateless
、@Singleton
和 @MessageDriven
可以通过 @Inject
注入范围引用,但它们不能成为 @RequestScoped
或任何其他范围.只有 @Stateful
模型足够灵活以支持作用域.换句话说,您可以将 @Stateful
bean 类本身注释为 @RequestScoped
、@SessionScoped
等.
While @Stateless
, @Singleton
and @MessageDriven
can have scoped references injected via @Inject
, they cannot be @RequestScoped
or any other scope. Only the @Stateful
model is flexible enough to support scopes. In other words, you can annotate the @Stateful
bean class itself as @RequestScoped
, @SessionScoped
, etc..
简单来说@Stateless
,@Singleton
有固定的作用域";已经.@Singleton
本质上是 @ApplicationScoped
并且 @Stateless
可能是一些像 @InvocationScoped
这样的虚构作用域,如果那个存在.@MessageDriven
bean 的生命周期完全取决于驱动它的连接器,因此也不允许具有用户定义的范围.
In simple terms @Stateless
, @Singleton
have fixed "scopes" already. @Singleton
is essentially @ApplicationScoped
and @Stateless
would perhaps be some made-up scope like @InvocationScoped
, if that existed. The lifecycle of an @MessageDriven
bean is entirely up to the Connector that drives it and is therefore also not allowed to have user-defined scope.
这篇关于是否可以将@RequestScoped bean @Inject 到@Stateless EJB 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!