如果没有 Web 层并因此没有 HTTP 会话,CDI 是否有意义?
新的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这让我想起我自己不久前问过的问题: @SessionScoped 如何与 EJB 配合使用? CDI 仅适用于 Web 层吗?
似乎“范围”的概念仅在 HTTP 会话的情况下相关。
但是,我可以看到 @ApplicationScoped 范围的有效使用作为实现应用程序单例 bean 的一种方式,无论请求是否是 HTTP 请求。
Javadoc 说:
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:
您还可以创建自己的范围。 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.
除此之外,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.