Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I override the value supplied to a field annotated with @EJB?
    primarykey
    data
    text
    <p>Suppose I have a class like this:</p> <pre><code>@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 } </code></pre> <p>And elsewhere I have:</p> <pre><code>@Stateless @Local({ Y.class }) public class YBean implements Y {} @Stateless @Local({ Y.class }) public class UnwantedYBean implements Y {} </code></pre> <p>Suppose now I want (in an XML descriptor, using the minimal amount of XML) to override or explicitly specify what gets placed in the <code>y</code> field in <code>XBean</code>.</p> <p>This construct (in a <code>META-INF/ejb-jar.xml</code> file, of course) surprisingly (to me) does <em>not</em> work in GlassFish 3.1.2.2:</p> <pre><code>&lt;ejb-jar&gt; &lt;enterprise-beans&gt; &lt;session&gt; &lt;ejb-name&gt;XBean&lt;/ejb-name&gt; &lt;ejb-local-ref&gt; &lt;ejb-ref-name&gt;foo.XBean/y&lt;/ejb-ref-name&gt; &lt;ejb-link&gt;YBean&lt;/ejb-link&gt; &lt;/ejb-local-ref&gt; &lt;/session&gt; &lt;/enterprise-beans&gt; &lt;/ejb-jar&gt; </code></pre> <p>I'd like to focus this question on the <code>ejb-jar.xml</code> snippet above, not on any container bugs or anything like that. Specifically: <strong>is the above snippet the correct and the smallest possible way to override what EJB gets injected into my <code>XBean.y</code> field?</strong></p> <p>Some notes:</p> <ul> <li><p>Others have suggested that I need to put an <code>&lt;injection-target&gt;</code> stanza in there. Why would that be? The injection target is already specified by virtue of my using the <code>@EJB</code> annotation. I don't wish to override where the injection occurs, only what actually gets injected.</p></li> <li><p>The EJB specification author herself also said that the <code>&lt;ejb-ref-name&gt;</code> should be simply <code>y</code>, not <code>foo.XBean/y</code>. This is despite the fact that the specification says (section 16.5.1.3):</p></li> </ul> <blockquote> <p>The ejb-ref-name element specifies the EJB reference name: <em>its value is the environment entry name used in the enterprise bean code</em> [emphasis mine].</p> </blockquote> <ul> <li><p>There is nowhere in the EJB specification that says what the default value of the <code>name</code> attribute is for the <code>@EJB</code> annotation (!), but we can infer that it is also the environment entry name. </p> <p>The spec gives an example in section 16.5.1.1:</p></li> </ul> <pre><code>package com.acme.example; @Stateless public class ExampleBean implements Example { ... @EJB private ShoppingCart myCart; ... }</code></pre> <p>...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 <code>@EJB</code>'s <code>name</code> attribute is):</p> <blockquote> <p>The enterprise bean reference will have the name <code>java:comp/env/com.acme.example.ExampleBean/myCart</code> in the referencing bean’s naming context, where <code>ExampleBean</code> is the name of the class of the referencing bean and <code>com.acme.example</code> its package.</p> </blockquote> <p>The "naming context" in that sentence is the <code>java:comp/env/</code> part, so everything else is the name, so the default value of an unadorned <code>@EJB</code> annotation's <code>name</code> attribute is <code>classname/fieldName</code>. I don't see how this could be otherwise. This is also backed up by the table present in <a href="https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html" rel="nofollow">the GlassFish EJB FAQ</a>.</p> <p>Bottom line: <strong>what is wrong with my XML stanza cited above?</strong> Why does it not cause a proxied instance of <code>YBean</code> to be injected into my <code>XBean</code>'s <code>@EJB</code>-annotated <code>private Y y</code> field?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload