是否可以@Inject一个@RequestScoped bean到@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 bean的类似问题 - 基本上想知道关于@Stateless的相同问题。
I also asked a similar question about JMS @MessageDriven beans - basically want to know the same about @Stateless.
@RequestScoped CDI注入@MessageDriven bean
推荐答案
你绝对可以做你提到的并使用 @RequestScoped
@Stateless
会话bean中的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.
这篇关于是否可以@Inject一个@RequestScoped bean到@Stateless EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!