在 Java EE6 中在类级别声明 @Resource 和 @EJB [英] Declaring @Resource and @EJB at the class level in Java EE6

查看:25
本文介绍了在 Java EE6 中在类级别声明 @Resource 和 @EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有什么情况吗(鉴于Java EE6有java:global/、app/、module/命名标准)需要声明 EJB 或资源,如下例所示?

Is there any situation still ( given that Java EE6 has java:global/, app/, module/ naming standards) that necessitates declaring EJBs or Resources like the example below?

@EJB (name = "ejb/PlaceBid", beanInterface = PlaceBid.class)
public class ActionBazaarBidControllerServlet extends HttpServlet {

}

ActionBazaarBidControllerServlet

PlaceBid placeBid = (PlaceBid)context.lookup("java:comp/env/ejb/PlaceBid");

推荐答案

java:comp/env/ 命名空间有时是一个不太容易理解的功能.此命名空间对应于所谓的企业命名上下文 (ENC).

The java:comp/env/ namespace is sometimes a little understood feature. This namespace corresponds to what is called the Enterprise Naming Context (ENC).

它就像一个与每个组件相关联的私有hashmap",整个 Web 模块被视为一个组件,而各个 EJB bean 也是组件.

It's like a private "hashmap" associated with each component, with the entire web module being treated as one component and individual EJB beans each being components as well.

您将此私有命名空间主要用于别名.如果您在ejb/PlaceBid"下映射某些内容,则所有(帮助程序)代码都可以使用ejb/PlaceBid",并且在一个全局位置(在本例中为 servlet,但也可以是 XML),您可以确定确切映射的内容到它.如果所有代码都直接转到 java:global/...,那么可能会有几十个硬编码依赖项到这个名称.

You use this private namespace mostly for aliasing purposes. If you map something under "ejb/PlaceBid", then all (helper) code can use "ejb/PlaceBid" and at one global location (the servlet in this case, but it could also be XML) you can determine what is exactly mapped to it. If all code went directly to java:global/... then there might be dozens of hardcoded dependencies to this name.

因此,如果您需要额外的重定向,您可以使用它.

So, you would use this if you need the extra redirection.

其他一些注意事项:

1.使用 EJB SessionContext,您可以直接引用此命名空间,如下所示:

1. With the EJB SessionContext you can reference this namespace directly, as-in:

PlaceBid placeBid = (PlaceBid)sessionContext.lookup("ejb/PlaceBid");

此处的ejb/PlaceBid"是 ENC 中的相对名称.

Here "ejb/PlaceBid" is a relative name into the ENC.

2.如前所述,每个组件的 ENC 都是私有的.不同的 EJB 可以具有不同映射的给定相对名称.context.lookup("java:comp/env/ejb/PlaceBid") 因此可以根据您调用的组件返回不同的内容.

2. As mentioned, the ENC is private for each component. Different EJBs can have a given relative name mapped differently. context.lookup("java:comp/env/ejb/PlaceBid") can thus return something different depending on from which component you make the call.

3.从历史上看,ENC 早于我们现在所说的注入.在某种程度上,当您在 XML 中指定映射时,您将某些内容注入"到与该组件关联的私有hashmap"中.现代版本注入字段、构造函数或属性(setter),但旧版本注入键".

3. Historically, the ENC predated what we now call injection. In a way, when you specified a mapping in XML, you 'injected' something into the private "hashmap" associated with that component. The modern version injects into fields, constructors or properties (setters), but the old version injected into 'keys'.

即使在 EJB3.1 中,历史上的注入"机制仍然在幕后活动.如果您在字段上使用@EJB 执行看似正常的注入,那么这也会自动在 JNDI ENC 中创建一个条目.例如

Even in EJB3.1, the historical 'injection' mechanism is still active under the hood. If you perform a seemingly normal injection using @EJB on a field, then this also automatically creates an entry in the JNDI ENC. E.g.

package test;

@Stateless
public class MyBean {

   @EJB
   private MyService myService;

}

在这种情况下,注入到 myService 中的任何内容也以 java:comp/env/test.MyBean/myService 的名称存储在 ENC 中.要使用您在 Servlet 上使用的 @EJB 批注完成链接:您可以选择使用 name 属性来指定在 ENC 中存储引用的名称:

In this case, whatever is injected into myService is also stored in the ENC under name java:comp/env/test.MyBean/myService. To complete the link with the @EJB annotation you used on the Servlet: you can optionally use the name attribute to specify the name under which the reference is stored in the ENC:

@Stateless
public class MyBean {

   @EJB(name = "ejb/PlaceBid")
   private MyService myService;

}

有点违反直觉,但在大多数情况下,这里的 name 属性不是指向要注入的对象来自的的东西,但它在 ENC 中用作目标注入.

A little counter-intuitive, but in most cases the name attribute here is thus not something that points to a source where the object to be injected is taken from, but it's used as a target in the ENC to inject into.

这篇关于在 Java EE6 中在类级别声明 @Resource 和 @EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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