Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Referencing a bean in another jar (with annotations)

When using annotations to reference a bean from another ejb in your ear you have to supplement the @EJB reference with a small chunk of xml in the ejb-jar.xml of the referring bean.

So in ejb app A colorsApp.jar you have this bean:

Code Block
package com.foo.colors;

import javax.ejb.Stateless;

@Stateless
public class OrangeBean implements OrangeRemote {
}

Then in ejb app B shapesApp.jar you have this bean with a reference to OrangeRemote:

Code Block
package com.foo.shapes;

import javax.ejb.Stateless;
import com.foo.colors.OrangeRemote;

@Stateless
public class SquareBean implements SquareRemote {
    @EJB OrangeRemote orangeRemote;
}

To hook this reference up you need to override this ref and add more info in the ejb-jar.xml of shapesApp.jar as follows:

Code Block
xml
xml
<ejb-jar>
  <enterprise-beans>

    <session>
      <ejb-name>SquareBean</ejb-name>
      <ejb-ref>
        <ejb-ref-name>com.foo.shapes.SquareBean/orangeRemote</ejb-ref-name>
        <ejb-link>colorsApp.jar#OrangeBean</ejb-link>
      </ejb-ref>
    </session>

  </enterprise-beans>
</ejb-jar>

Referencing a bean in another jar (xml only, no annotations)

The same basic approach applies and dependency injection is still possible, however more information must be described in the xml.

In ejb app A colorsApp.jar you have this bean:

Code Block
package com.foo.colors;

import javax.ejb.Stateless;

@Stateless
public class OrangeBean implements OrangeRemote {
}

Then in ejb app B shapesApp.jar – note there is no @EJB annotation:

Code Block
package com.foo.shapes;

import javax.ejb.Stateless;
import com.foo.colors.OrangeRemote;

@Stateless
public class SquareBean implements SquareRemote {
    OrangeRemote orangeRemote;
}

Here's how you would hook this reference up, injection and all, with just xml. The following would be added to the ejb-jar.xml of shapesApp.jar:

Code Block
xml
xml
<ejb-jar>
  <enterprise-beans>

    <session>
      <ejb-name>SquareBean</ejb-name>
      <ejb-ref>
        <ejb-ref-name>com.foo.shapes.SquareBean/orangeRemote</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <remote>com.foo.colors.OrangeRemote</remote>
        <ejb-link>colorsApp.jar#OrangeBean</ejb-link>
        <injection-target>
          <injection-target-class>com.foo.shapes.SquareBean</injection-target-class>
          <injection-target-name>orangeRemote</injection-target-name>
        </injection-target>
      </ejb-ref>
    </session>

  </enterprise-beans>
</ejb-jar>

Note that the value of <ejb-ref-name> could actually be anything and the above example would still work as there is no annotation that needs to match the <ejb-ref-name> and no one will likely be looking up the EJB as it's injected.