@EJB 和@Resource 有什么区别?在 EJB 中的依赖注入

问题描述:

我想知道 EJB @EJB 和 @Resource 注解的主要区别是什么?在这种情况下,我们必须使用 @EJB 和 @resource

I like to know what is the main difference between EJB @EJB and @Resource annotation? In which case we have to go for @EJB and @resource

规范状态,关于@EJB:

Specification states, regarding @EJB:

Bean Provider 使用 EJB 注释来注释字段或设置器作为注入目标的 bean 类的属性方法EJB 参考.引用可能是会话 bean 的业务接口或本地 home 接口或远程 home 接口会话 bean 或实体 bean.

The Bean Provider uses the EJB annotation to annotate a field or setter property method of the bean class as a target for the injection of an EJB reference. The reference may be to a session bean’s business interface or to the local home interface or remote home interface of a session bean or entity bean.

关于@Resource(第 16.2.2 节):

regarding @Resource(section 16.2.2):

bean 类的一个字段或方法可以被注解以请求一个来自 bean 环境的条目被注入.任何类型的本章中描述的资源或其他环境条目可能被注射.

A field or method of a bean class may be annotated to request that an entry from the bean’s environment be injected. Any of the types of resources or other environment entries described in this chapter may be injected.

提及的条目包括:EJB 引用、Web 服务引用、资源管理器连接工厂引用、消息目标引用、单元引用、持久性上下文引用、UserTransaction、CORBA ORB 对象、TimerService、EJBContext 对象

Mentioned entries include: EJB reference, web service reference, resource manager connection factory reference, message destination reference, unit reference, persistence context reference, UserTransaction, CORBA ORB object, TimerService, EJBContext object