@EJBs 注释有什么作用? [英] What does the @EJBs annotation do?

查看:20
本文介绍了@EJBs 注释有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大致知道这个构造的作用:它创建一个 SomeType EJB 并将对象注入另一个 EJB.

I know roughly what this construction does: it creates a SomeType EJB and injects the object into another EJB.

 @EJB(name="name1")
 SomeType someVariable

现在我有一个这样开头的类:(我给出了所有类级别的注释,尽管我认为只有 @EJBs 是相关的)

Now I have a class which starts like this: (I give all class-level annotations, even though I think only the @EJBs is relevant)

@Remote(SomeClass.class)
@Stateless(name="someName")
@EJBs({@EJB(name="name1",beanInterface=Type1.class),
       @EJB(name="name2",beanInterface=Type2.class)})
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class X extends Y{ 
  //code

@EJB 在这里做什么?他们可能从 JNDI 获取或创建name1"...对象,但是他们把结果放在哪里呢?我在附近的任何地方都没有看到 .lookup 调用,但代码库很大,所以我对此不太确定.

What do the @EJB s do here? They probably get or create the "name1" ... objects from JNDI, but where do they put the result? I don't see a .lookup call anywhere near, but the codebase is huge so I'm not very sure about this.

额外问题:我认为这两个 @Transaction 注释只是重复默认值?

Bonus question: I presume the two @Transaction annotations simply repeat defaults?

更新: 很多人在这一点上声称 @EJBs 是一个专有扩展.它不是.它是java EE5的核心部分.有关详细信息,请参阅 JavaDoc..它只是单个 @EJB 注释的容器.

UPDATE: Multiple persons claimed at this point that @EJBs is a proprietary extension. It is not. It is a core part of java EE5. See the JavaDoc for details.. It is simply a container for the individual @EJB annotations.

我相信每个声称拥有这些 EJB 注释的人都会进行查找.我只想知道这个查找的结果会发生什么.

I believe everyone who claims these EJB annotations do a lookup. I just want to know what happens with the result of this lookup.

推荐答案

@EJB 注释(和 @Resource@WebServiceRef、等)有两个目的:

The @EJB annotation (and @Resource, @WebServiceRef, etc.) serves two purposes:

  1. 它在组件命名空间中声明了一个引用.例如,@EJB(name="myEJB") 创建一个引用 java:comp/env/myEJB.如果您对字段进行注释但未指定名称,则会创建一个引用 java:comp/env/com.example.MyClass/myField.
  2. 如果注解是在字段或 setter 方法上声明的,则容器在创建组件时执行注入.
  1. It declares a reference in the component namespace. For example, @EJB(name="myEJB") creates a reference java:comp/env/myEJB. If you annotate a field and do not specify a name, then it creates a reference java:comp/env/com.example.MyClass/myField.
  2. If the annotation is declared on a field or setter method, then the container performs injection when the component is created.

引用的解析方式各不相同,与引用是为 lookup("java:comp/env/myEJB") 解析还是由于注入无关:

How the reference is resolved varies, independent of whether the reference is being resolved for a lookup("java:comp/env/myEJB") or due to injection:

  1. 如果使用 EE 6+,lookup 属性需要 JNDI 查找来解析目标.
  2. 某些应用服务器支持 mappedName,它被指定为特定于供应商的.这通常通过执行查找来实现.
  3. 应用服务器在部署时支持绑定.这通常通过执行查找来实现.
  4. 如果没有提供其他绑定信息并且 bean 接口(beanInterface 或字段类型)仅由应用程序中的单个 EJB 实现,则 EJB 规范要求它回退到那个.
  5. 如果没有提供其他绑定信息并且 #4 无法工作,一些应用服务器将尝试根据引用名称在服务器命名空间中执行查找(例如,java:comp/env/myEJBjava:comp/env/myEJBcode> 可能会导致在服务器命名空间中查找 myEJB).
  1. If EE 6+ is used, the lookup attribute requires a JNDI lookup to resolve the target.
  2. Some application servers support mappedName, which is specified to be vendor specific. This is usually implemented by performing a lookup.
  3. Application servers support bindings at deployment time. This is usually implemented by performing a lookup.
  4. If no other binding information is provided and the bean interface (beanInterface or the field type) is only implemented by a single EJB in the application, then the EJB specification requires that it fall back to that.
  5. If no other binding information is provided and #4 cannot work, some application servers will attempt to perform a lookup in the server namespace based on the ref name (for example, java:comp/env/myEJB might cause a lookup of myEJB in the server namespace).

这篇关于@EJBs 注释有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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