如何覆盖提供给使用@EJB注释的字段的值? [英] How do I override the value supplied to a field annotated with @EJB?

查看:125
本文介绍了如何覆盖提供给使用@EJB注释的字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的类:

  @Stateless 
@Local({X.class})
public class XBean实现X {
@EJB // name()属性应该默认为foo.XBean / y,但见下面
private Y y; // Y是@Local
}

其他地方我有:

  @Stateless 
@Local({Y.class})
public class YBean implements Y {}

@Stateless
@Local({Y.class})
public class UnwantedYBean实现Y {}

假设现在我想要(在XML描述符中使用最少的XML)来覆盖或明确指定放在 y 字段中的内容在 XBean



此构造(在$ code> META-INF / ejb-jar中。 xml 文件,当然)令人惊讶的(对我来说)不是在GlassFish 3.1.2.2中工作:

 < EJB-JAR> 
< enterprise-beans>
< session>
< ejb-name> XBean< / ejb-name>
< ejb-local-ref>
< ejb-ref-name> foo.XBean / y< / ejb-ref-name>
< ejb-link> YBean< / ejb-link>
< / ejb-local-ref>
< / session>
< / enterprise-beans>
< / ejb-jar>

我想将这个问题集中在 ejb-jar.xml上上面的片段,不在任何容器bug或任何类似的东西。具体来说:是上面的代码片段,正确和最小的方法来覆盖EJB注入到我的 XBean.y 字段中的方法



一些注释:




  • 其他人建议我需要放一个code>< injection-target> 节在那里。为什么会这样?注射目标已经通过使用 @EJB 注释来指定。我不希望覆盖注入发生的地方,只是实际注入了。


  • EJB规范作者自己也说,< ejb-ref-name> 应该只是 y ,而不是 foo.XBean / y 。这是尽管规范说明(第16.5.1.3节):





ejb-ref-name元素指定EJB引用名称:其值是企业bean代码中使用的环境条目名称 [emphasis mine]。





  • EJB规范中无处可见,名称的默认值属性用于 @EJB 注释(!),但是我们可以推断它也是环境条目名称。



    该规范在第16.5.1.1节提供了一个例子:




  package com.acme.example; 
@Stateless public class ExampleBean implements Example {
...
@EJB private ShoppingCart myCart;
...
}

...这完全等于我的,然后说在同一部分(这是我们将要发现的$ code的默认值@EJB 的名称属性是):


企业bean引用将具有名称 java:comp / env / com。引用bean的命名上下文中的acme.example.ExampleBean / myCart ,其中 ExampleBean 是引用bean的类的名称, com.acme.example 其包。


该句子中的命名上下文 java:comp / env / 部分,所以其他的都是名字,所以默认值为$ de code> @EJB 注释的名称属性是 classname / fieldName 。我看不出怎么会这样。这也由 GlassFish EJB常见问题解答中的表格备份。



底线:我上面引用的XML节是什么问题?为什么不会导致代理的注入我的 XBean @EJB -annotated 私人Y y 字段?

解决方案

1。


其他人建议我需要在这里放一个< injection-target>
。为什么会这样?


我同意您的解释,这是没有必要的。我最熟悉WebSphere Application Server,其实现也与您的解释一致。



2。


EJB规范作者自己也说,
应该只是y,而不是foo.XBean / y。


我不同意这种解释,并同意你的看法。



3。


EJB规范中没有地方说,name属性的默认
值是用于@EJB注释(!),但是我们可以
推断它也是环境条目名称。


EJB 3.1第16.5.2.1节说:


以下规则适用于如何部署描述符条目
覆盖EJB注释:




  • 相关部署描述符条目基于与注释一起使用的JNDI
    名称(两者都是defaulte) d或明确提供
    )。



Suppose I have a class like this:

@Stateless
@Local({ X.class })
public class XBean implements X {
  @EJB // name() attribute should by spec default to "foo.XBean/y", but see below
  private Y y; // Y is @Local
}

And elsewhere I have:

@Stateless
@Local({ Y.class })
public class YBean implements Y {}

@Stateless
@Local({ Y.class })
public class UnwantedYBean implements Y {}

Suppose now I want (in an XML descriptor, using the minimal amount of XML) to override or explicitly specify what gets placed in the y field in XBean.

This construct (in a META-INF/ejb-jar.xml file, of course) surprisingly (to me) does not work in GlassFish 3.1.2.2:

<ejb-jar>
 <enterprise-beans>
  <session>
   <ejb-name>XBean</ejb-name>
   <ejb-local-ref>
    <ejb-ref-name>foo.XBean/y</ejb-ref-name>
    <ejb-link>YBean</ejb-link>
   </ejb-local-ref>
  </session>
 </enterprise-beans>
</ejb-jar>

I'd like to focus this question on the ejb-jar.xml snippet above, not on any container bugs or anything like that. Specifically: is the above snippet the correct and the smallest possible way to override what EJB gets injected into my XBean.y field?

Some notes:

  • Others have suggested that I need to put an <injection-target> stanza in there. Why would that be? The injection target is already specified by virtue of my using the @EJB annotation. I don't wish to override where the injection occurs, only what actually gets injected.

  • The EJB specification author herself also said that the <ejb-ref-name> should be simply y, not foo.XBean/y. This is despite the fact that the specification says (section 16.5.1.3):

The ejb-ref-name element specifies the EJB reference name: its value is the environment entry name used in the enterprise bean code [emphasis mine].

  • There is nowhere in the EJB specification that says what the default value of the name attribute is for the @EJB annotation (!), but we can infer that it is also the environment entry name.

    The spec gives an example in section 16.5.1.1:

package com.acme.example;
@Stateless public class ExampleBean implements Example {
  ...
  @EJB private ShoppingCart myCart;
  ...
}

...which is exactly equal to mine, and then says in the same section (this is as close as we'll get to discovering what the default value for @EJB's name attribute is):

The enterprise bean reference will have the name java:comp/env/com.acme.example.ExampleBean/myCart in the referencing bean’s naming context, where ExampleBean is the name of the class of the referencing bean and com.acme.example its package.

The "naming context" in that sentence is the java:comp/env/ part, so everything else is the name, so the default value of an unadorned @EJB annotation's name attribute is classname/fieldName. I don't see how this could be otherwise. This is also backed up by the table present in the GlassFish EJB FAQ.

Bottom line: what is wrong with my XML stanza cited above? Why does it not cause a proxied instance of YBean to be injected into my XBean's @EJB-annotated private Y y field?

解决方案

1.

Others have suggested that I need to put an <injection-target> stanza in there. Why would that be?

I agree with your interpretation that it's unnecessary. I'm most familiar with WebSphere Application Server, and its implementation also agrees with your interpretation.

2.

The EJB specification author herself also said that the should be simply y, not foo.XBean/y.

I disagree with that interpretation and agree with yours.

3.

There is nowhere in the EJB specification that says what the default value of the name attribute is for the @EJB annotation (!), but we can infer that it is also the environment entry name.

EJB 3.1 section 16.5.2.1 says:

The following rules apply to how a deployment descriptor entry may override an EJB annotation:

  • The relevant deployment descriptor entry is located based on the JNDI name used with the annotation (either defaulted or provided explicitly).

这篇关于如何覆盖提供给使用@EJB注释的字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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