users@glassfish.java.net

Re: lookup ejbs

From: <glassfish_at_javadesktop.org>
Date: Tue, 14 Oct 2008 14:41:41 PDT

> java:comp/env/app.ejb.session.PersonManagerBean/entity
> Manager;|naming.bind|#]

This is a default name assigned to the dependency if the name() attribute of
@PersistenceContext is not specified. It guarantees the uniqueness of the dependency name
but it can easily be overridden. In most cases the code doesn't need to reference it.

It's very easy to define a simple lookup name that allows you to retrieve an EJB reference
anywhere in the web app, without using web.xml. Just add an @EJB annotation at the
class level of any managed class in the .war. The easiest would be a servlet class.

E.g.

@EJB(name="myejbref", beanInterface=<LocalInterface>.class)
public class SomeServlet ... { ... }

Then, anywhere in the web application, look up the local EJB reference via

  <LocalInterface> ref = (<LocalInterface>)
         new InitialContext().lookup("java:comp/env/myejbref");

Note that the portion of the string after "java:comp/env/" matches the contents of the @EJB name() attribute. This is true of any Java EE environment annotation : @EJB, @PersistenceContext, @PersistenceUnit, @Resource, etc.
[Message sent by forum member 'ksak' (ksak)]

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