是否可以将 @RequestScoped bean @Inject 到 @Stateless EJB 中?

发布于 2024-12-24 01:45:08 字数 521 浏览 5 评论 0原文

是否可以将请求范围的 CDI bean 注入无状态会话 bean 中?

我问了一个相关的问题,并认为特定的 CDI @RequestScoped into @Stateless 问题值得单独发表。

在 EJB 方法/@RequestScoped 和 @Stateless 之间传递状态

还问了有关 JMS @MessageDriven beans 的类似问题 - 基本上想了解有关 @Stateless 的相同问题。

@RequestScoped CDI 注入@MessageDriven bean

Is it possible to inject a request-scoped CDI bean into a Stateless session bean?

I had asked a related question and thought the specific CDI @RequestScoped into @Stateless question merited its own post.

Passing state between EJB methods / @RequestScoped and @Stateless

I also asked a similar question about JMS @MessageDriven beans - basically want to know the same about @Stateless.

@RequestScoped CDI injection into @MessageDriven bean

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

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

发布评论

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

评论(1

青春如此纠结 2024-12-31 01:45:08

您绝对可以执行您提到的操作,并在 @Stateless 会话 bean 和 @MessageDriven bean 中使用 @RequestScoped bean。这是 CDI 规范和 TCK 的核心部分,并保证可移植。

关于 MDB 的注意事项

请注意,有一个针对使用 @RequestScoped bean 的 @Stateless bean 的测试,但没有测试保证 @MessageDriven bean 可以引用 @RequestScoped bean。这只是一个疏忽,已针对 Java EE 7 TCK 修复。因此,请注意,如果它不适用于 MDB 情况,则可能不是您的错:)

解决方法是简单地将您的 MDB 委托给任何类型的 SessionBean 作为 @Stateless@Stateful@Singleton 都有 @RequestScoped 测试。

使 EJB 本身具有作用域

@Stateless@Singleton@MessageDriven 可以通过 @Inject 注入作用域引用code>,它们不能是 @RequestScoped 或任何其他范围。只有 @Stateful 模型足够灵活以支持范围。换句话说,您可以将 @Stateful bean 类本身注释为 @RequestScoped@SessionScoped 等。

简单来说 @Stateless@Singleton 已经固定了“范围”。 @Singleton 本质上是 @ApplicationScoped ,而 @Stateless 可能是一些虚构的作用域,例如 @InitationScoped,如果那是存在的。 @MessageDriven bean 的生命周期完全取决于驱动它的连接器,因此也不允许具有用户定义的范围。

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.

Note on MDBs

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 :)

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.

Making the EJB, itself, scoped

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..

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.

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