ejb@glassfish.java.net

Re: [Fwd: [Fwd: Please update EJB FAQ on the site]]

From: Mahesh.Kannan <Mahesh.Kannan_at_Sun.COM>
Date: Wed, 11 Apr 2007 17:45:53 -0700

> The faq located at
> https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB
> was worthless.
>
> Please provide links to fully functional examples for each question
> answered. The syntax provided isn't even correct:
>
> If the POJO is called from a web application, the java:comp/env
> namespace is shared by the entire .war. So, the local EJB dependency
> can be defined by using an @EJB annotation on any managed class within
> the .war.
>
> E.g.
>
> @EJB(name="fooejbref", beanInterface=FooLocal.class)
> public class MyServlet extends HttpServlet { ...
>
> Excuse me? @EJB, as far as any documentation I've seen, is meant to
> apply to a variable defined in the HttpServlet subclass for DI, not to
> the servlet class definition.

The syntax provided is correct. The Target annotation in javax.ejb.EJB
annotation clearly tells that the @EJB annotaiton can be applied to
FIELD, METHOD or TYPE (Which is the class). So this is correct.

When @EJB is used at a field level, it not only tells that the field is
the target of injection but also declares a dependency on the component
namespace (which can be looked up using (new
InitialContext()).lookup("..."))

When @EJB is used at class level (or TYPE level) it simply declares a
dependency on the compoent namespace. For example:

@EJB(name="ejb/fooejbref", beanInterface=FooLocal.class)
public class MyServlet extends HttpServlet {
     ....
     doPost(....) {
          (new InitialContext()).lookup("java:comp/env/ejb/fooejbref")
     }
}

In the above code, the lookup works because the dependency was defined
by the use of @EJB at the class Level. If however, the @EJB was
defined at a field level inside the Servlet, then it not only declares
the dependecy but also declares that this field is the target of
dependency injection.

> And, what is meant by "any managed class"?

Actually the FAQ says: " If the POJO is called from a web application,
the java:comp/env namespace is shared by the entire .war. So, the local
EJB dependency can be defined by using an @EJB annotation on any managed
class within the .war. "

which simply means that: by using the @EJB at the class level (or the
TYPE level) of a managed class (like a Servlet) the dependency can be
defined in the component's namespace.

Also, placing @EJB inside a POJO (which is a not a managed class) wont
work because the container doen't inject anything into it. However, the
POJO can lookup (initialContext.lookup()) if it is invoked (directly or
indirectly) from a managed component.

>
> This kind of documentation is very frustrating. I've been working for
> the last hour trying to get a reference to a @Local @Stateless bean
> from a POJO in a war, and I've had absolutely no luck. I haven't
> tried to use an xml descriptor to bind the bean, because, well, that's
> what annotations are for in the first place.
>
We have published a number of simple examples on our glassfish ejb
page. Please visit the following link for some sample programs:
 https://glassfish.dev.java.net/javaee5/ejb/EJB30.html

Thanks,
--Mahesh