users@glassfish.java.net

Re: EJB Reference from a Helper class in the same EJB module

From: <glassfish_at_javadesktop.org>
Date: Wed, 14 May 2008 08:30:26 PDT

BarBean is an EJB component. To look up a reference to a different Local EJB component from any non-managed class (POJO, utility class, etc.) that is called from a business method of BarBean, you would do the following :

@EJB(name="fooejbref", beanInterface=FooLocal.class)
@Stateless
public class BarBean implements BarLocal {

   public void bar() {
      Util util = new Util();
      util.foo();
   }

}

public class Util {
  public void foo() {
    FooLocal fooLocal = (FooLocal) new InitialContext("java:comp/env/fooejbref");
  }
}

The portion of the lookup string after "java:comp/env/" matches the "name" attribute of @EJB.
@EJB defines the ejb dependency so that it's available when the utility code does a lookup
from the context of a BarBean invocation. This @EJB is equivalent to defining an ejb-local-ref within the BarBean section of ejb-jar.xml.
[Message sent by forum member 'ksak' (ksak)]

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