如果没有 Web 层并因此没有 HTTP 会话,CDI 是否有意义?

发布于 2024-12-29 05:41:29 字数 239 浏览 5 评论 0原文

新的 JSR 299“Java EE 的上下文和依赖注入”似乎基于“范围”的概念。

这些 bean 被创建并关联到受支持的范围之一:应用程序、会话(映射到 HTTP 会话)、对话和请求。

如果没有 HTTP 会话(例如,通过 EJB 远程处理公开功能的企业应用程序),那么使用 CDI 是否有意义,因为托管 Bean 不会与任何上下文关联(因为它们不存在)?

在这种情况下是否可以使用 CDI?会给它带来哪些好处呢?

The new JSR 299 "Contexts and Dependency Injection for Java EE" seems to be based on the concept of "Scope".

The beans are created and associated to one of the supported Scopes: Application, Session (mapped to a HTTP session), Conversation, and Request.

Does it make sense to use CDI if there is no HTTP Session (for example, an Enterprise application that exposes functionality through EJBs remoting) since the Managed Beans are not going to be associated to any Context (since they do not exist)?

Is it even possible to use CDI in such an scenario? Which advantages would it bring to it?

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

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

发布评论

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

评论(3

我早已燃尽 2025-01-05 05:41:29

这让我想起我自己不久前问过的问题: @SessionScoped 如何与 EJB 配合使用? CDI 仅适用于 Web 层吗?

似乎“范围”的概念仅在 HTTP 会话的情况下相关。
但是,我可以看到 @ApplicationScoped 范围的有效使用作为实现应用程序单例 bean 的一种方式,无论请求是否是 HTTP 请求。

Javadoc 说:

应用程序范围处于活动状态:

(...)

  • 在任何 Java EE Web 服务调用期间,

  • 在任何 EJB 的任何远程方法调用期间、在任何 EJB 的任何异步方法调用期间、在对 EJB 的任何调用期间
    超时方法以及在消息传递到任何 EJB 消息驱动期间
    豆子,

It reminds me of my own question I asked some time ago: How does @SessionScoped work with EJB? Is CDI only for web-tier?

It seems that the idea of 'scope' is relevant only in the case of HTTP Session.
However, I can see a valid use of the @ApplicationScoped scope as a way to implement an application-singleton bean, despite if the request is a HTTP one.

Javadoc says:

The application scope is active:

(...)

  • during any Java EE web service invocation,

  • during any remote method invocation of any EJB, during any asynchronous method invocation of any EJB, during any call to an EJB
    timeout method and during message delivery to any EJB message-driven
    bean,

永言不败 2025-01-05 05:41:29

您还可以创建自己的范围。 CDI 具有很强的可扩展性,可以在多种情况下使用。它也被用在既没有 HttpSession 也没有 HttpRequest 的 SE 应用程序中。

You can also create your own scopes. CDI is very extensible and can be used in a variety of situations. It's also being used in SE applications where there is neither an HttpSession nor HttpRequest.

您的好友蓝忘机已上羡 2025-01-05 05:41:29

除此之外,CDI 不仅用于生命周期管理,您还可以使用它进行依赖项注入,从而以非常干净的方式将接口与其实现分离。您还可以使用拦截器和装饰器执行一些 AOP 技术,或者利用 CDI 事件构建非常松散耦合的观察者模式。

Apart from that, CDI is not only for Lifecycle managment, you can use it to do dependency injection thus separating interfaces from their implementations in a very clean way. You can also perform some AOP techniques using interceptors and decorators, or build a very loose-coupled Observer pattern by taking advantage of CDI Events.

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