CDI和EJB如何比较?相互作用? [英] How do CDI and EJB compare? interact?

查看:248
本文介绍了CDI和EJB如何比较?相互作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解两者之间的相互作用以及它们之间的界限。它们重叠吗?他们之间是否有冗余?



我知道有两个注释相关联,但是我没有能够找到一个完整的列表,简要描述。不知道这是否有助于清除它们如何不同或重叠的位置。



真的只是困惑。我(认为我)理解EJB相当好,我想我很难理解CDI给表带来什么,以及如何取代或增强EJB已经提供的内容。

解决方案

CDI - 它是关于依赖注入。这意味着您可以在任何地方注入接口实现。这个对象可以是任何东西,它可以与EJB无关。 这里是如何使用CDI注入随机生成器的示例。没有什么关于EJB的。当您想要注入非EJB服务,不同的实现或算法时,您将使用CDI(所以您根本不需要EJB)。

您明白了,可能您感到困惑通过@EJB注释 - 它允许您将实现注入到您的服务或任何。主要思想是,您注入的类应由EJB容器管理。
看起来CDI确实知道EJB是什么,所以在Java EE 6兼容的服务器中,在你的servlet中,你可以写

  @EJB EJBService ejbService; 

  @Inject EJBService ejbService; 

这可以让你感到困惑,但这可能是EJB和CDI之间的桥梁



当我们谈论CDI时,您可以将其他对象注入CDI托管类(他们应该由CDI感知框架创建)。



其他CDI提供什么...例如,您使用Struts 2作为MVC框架(仅作为示例),并且您在这里受到限制,甚至使用EJB 3.1 - 您不能使用@EJB注释在Struts操作中,它不是由容器管理的。但是当您添加Struts2-CDI插件时,您可以在同一件事上写入@Inject注释(所以不需要更多的JNDI查找)。这样可以增强EJB的功能。但是正如我之前提到的,CDI注入的是什么 - 如果它与EJB有关,这并不重要,而这就是它的实力。



PS。更新的链接到示例


I'm having a tough time understanding how the two interact and where the boundary between them lies. Do they overlap? Are there redundancies between them?

I know there are annotations associated with both, but I haven't been able to find a complete list for both with brief descriptions. Not sure if this would help clear up how they differ or where they overlap.

Really just confused. I (think I) understand EJB reasonably well, I guess I'm having a hard time understanding exactly what CDI brings to the table and how it supplants or enhances what EJB already offers.

解决方案

CDI - it is about dependency injection. It means that you can inject interface implementation anywhere. This object can be anything, it can be not related to EJB. Here is an example of how to inject random generator using CDI. There is nothing about EJB. You are going to use CDI when you want to inject non-EJB services, different implementations or algorithms (so you don't need EJB there at all).
EJB you do understand, and probably you are confused by @EJB annotation - it allows you to inject implementation into your service or whatever. The main idea is that class, where you inject, should be managed by EJB container. Seems that CDI does understand what EJB is, so in Java EE 6 compliant server, in your servlet you can write both

@EJB EJBService ejbService;

and

@Inject EJBService ejbService;

that's what can make you confusing, but that's probably the only thing which is the bridge between EJB and CDI.

When we are talking about CDI, you can inject other objects into CDI managed classes (they just should be created by CDI aware frameworks).

What else CDI offers... For instance, you use Struts 2 as MVC framework (just example), and you are limited here, even using EJB 3.1 - you can't use @EJB annotation in Struts action, it is not managed by container. But when you add Struts2-CDI plugin, you can write there @Inject annotation for the same thing (so no more JNDI lookup needed). This way it enhances EJB power. But as I mentioned before, what you inject with CDI - it does not matter if it is related to EJB or not, and that's its power

PS. updated link to the example

这篇关于CDI和EJB如何比较?相互作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆