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

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

问题描述

是否还有任何情况(鉴于Java EE6具有java:global /,app /,module / naming
标准),需要声明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 = (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早于我们现在所说的 injection 。在某种程度上,当您在XML中指定映射时,您会将某些内容注入与该组件关联的私有hashmap。现代版本注入字段,构造函数或属性(setter),但旧版本注入'keys'。

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的任何内容也会被存储在ENC中名称 java:comp / env / test.MyBean / myService 。要完成与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天全站免登陆