users@glassfish.java.net

Re: Getting one EJB within another EJB

From: <glassfish_at_javadesktop.org>
Date: Tue, 05 Jun 2007 07:54:22 PDT

You can still use @EJB but you need to provide some extra information to identify the target
EJB since there is more than one EJB component that exposes the same business interface.
Each EJB component is assigned an ejb-name. Before EJB 3.0, the ejb-name was assigned
using the ejb-name element in ejb-jar.xml. When annotations are used in EJB 3.0, the ejb-name
is either specified using the name() attribute of @Stateful/_at_Stateless/_at_MesssageDriven or
if the name() attribute is not specified it defaults to the unqualified name of the bean class.

So, in your example there are two Stateful Session Beans, one with ejb-name SomeBean1 and
another with ejb-name SomeBean2.

Here's how the @EJB would look :

@EJB(beanName="SomeBean1")
private SomeInterface someBean;

or

@EJB(beanName="SomeBean2")
private SomeInterface someBean;

The @EJB annotation has an optional element called beanName() which can be used to
explicitly identify the target bean within the application using ejb-name. beanName() is
equivalent to the ejb-link element within ejb-ref/ejb-local-ref, so it can also be overridden
in the deployment descriptor if you don't want to hardcode the mapping.

E.g.

@EJB(name="someBean_ejblocalref")
private SomeInterface someBean;

Then, apply the bean mapping for this local EJB dependency in the deployment descriptor :

<ejb-local-ref>
  <ejb-ref-name>someBean_ejblocalref"</ejb-ref-name>
  <local>com.acme.SomeInterface</local>
  <ejb-link>SomeBean1</ejb-link>
</ejb-local-ref>

You can find more information on the relationship between @EJB and ejb-ref/ejb-local-ref
in our EJB FAQ :

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#EJB_ejb-ref_ejb_local_ref

 --ken
[Message sent by forum member 'ksak' (ksak)]

http://forums.java.net/jive/thread.jspa?messageID=220572