@Inject和@EJB有什么区别 [英] What is the difference between @Inject and @EJB

查看:270
本文介绍了@Inject和@EJB有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习新的Java EE 6组件模型,并对最新的依赖项注入机制感到困惑。所以这是我的问题:

I'm currently learning the new Java EE 6 component models and am confused with the latest dependency injection mechanism. So here are my questions:

1)@Inject和@EJB有什么区别

1) What is the difference between @Inject and @EJB

2)如果我有一个包含另一个POJO的简单POJO(其中一个是DAO代码),哪个是更好的选择:@Inject或@EJB?

2) If I have a simple POJO that contains another POJOs (which one of them is the DAO code), what would be the better choice: @Inject or @EJB?

可以我将@Inject和@EJB混合在一起?

Can I mix @Inject and @EJB?

一个例子是:


  • ClassA实现InterfaceA并具有
    的ClassA_Adaptor实例

  • ClassA implements InterfaceA and has an instance of ClassA_Adaptor

ClassA_Adaptor实现InterfaceA的实例
并具有ClassB实例

ClassA_Adaptor implements InterfaceAB and has an instance of ClassB

ClassB实现InterfaceB,并具有
一个ClassB_Adaptor实例和一个
实例DAO_ClassB

ClassB implements InterfaceB and has an instance of ClassB_Adaptor and an instance DAO_ClassB

ClassB_Adaptor实现InterfaceB
并具有ClassC实例

ClassB_Adaptor implements InterfaceB and has an instance of ClassC

ClassC实现接口InterfaceBC并具有
WebService_ClassC实例

ClassC implements InterfaceBC and has an instance of WebService_ClassC

DAO_ClassB将使用JPA 2.0
(@PersistenceContext)

DAO_ClassB will use JPA 2.0 (@PersistenceContext)

我想将它们全部注入

3)仅对某些操作使用事务处理而不对所有操作使用事务处理是否不好?

3) Is it a bad approach to only use transactional for certain operations but not for all?

例如:DAO_ClassB中的某些方法是您的典型查询,而其他方法是写入方法。不使用事务包装 READ方法是否很不好?

As an example: Some methods in DAO_ClassB are your typical query, while other methods are "write" methods. Is it bad to not wrap the "READ" methods with transaction?

据我所知,可以使用@EJB将DAO_ClassB与事务包装在一起(注入DAO_ClassB并使所有事务性方法)。我怎么能控制它?

To my understanding, the DAO_ClassB can be wrapped with transaction using @EJB (inject the DAO_ClassB and make all methods transactional). How can I control it?

很抱歉,由于我只知道Java EE 6新组件模型的一些零碎的部分问题,所以令人困惑。

Sorry if some of the questions are confusing because I know only bits and pieces of the Java EE 6 new component model.

推荐答案


  1. @EJB仅注入EJB,但是@Inject可用于注入POJO,而不是EJB。但是,@ Inject要求您的归档文件是BDA(对于EE 6,包含Beans.xml或在EE 7中隐式包含)。 @Inject还具有其他特定于CDI的功能(范围,拦截器等),但是这些功能会产生额外的开销。应用程序服务器支持指定@EJB绑定,以便部署者可以选择目标EJB,但是@Inject仅允许应用程序开发人员选择目标EJB(并且它必须存在于应用程序中)。

  1. @EJB injects EJBs only, but @Inject can be used to inject POJOs rather than EJBs. However, @Inject requires that your archive be a BDA (contain beans.xml for EE 6, or implicitly in EE 7). @Inject also has additional CDI-specific capabilities (scopes, interceptors, etc.), but those capabilities incur extra overhead. Application servers have support for specifying @EJB bindings so that a deployer can choose the target EJB, but @Inject only allows the application developer to choose the target EJB (and it must exist in the application).

如果目标不是EJB,则一定不能使用@EJB。

If the target is not an EJB, then you must not use @EJB.

这取决于您是否进行多个相互关联的查询,然后尝试制定业务决策。您需要了解隔离级别并将其考虑在内,即使是只读操作也是如此。

It depends whether you're making multiple inter-related queries and then attempting to make business decisions. You need to understand isolation levels and take them into consideration, even for read-only operations.

这篇关于@Inject和@EJB有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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