The FAQ is a set of answers to common implementation issues, not a tutorial.
The entry in question "How do I access a Local EJB from a POJO or
utility class?" is trying to discuss what makes a local EJB different
than a Remote EJB to a POJO. It answers that question well (and the code
examples are correct).
Maybe a new Q&A should be added along the lines of:
Q: Can annotations do everything for me?
A:
Annotations provide 2 pieces of functionality, an alternate source of
configuration data (instead of xml files), and a request for dependency
injection.
In Java EE most annotations are only looked for in managed objects. A
managed object is an object that has it's lifecycle controlled by the
server (the server does the "new"). The primary managed object types
that would contain annotations are servlets and EJBs.
A POJO is not managed by the server therefore you do not place EJB
annotations inside of it. You can but they wouldn't be used. For a POJO
to access a EJB a JNDI lookup must be performed OR the EJB reference
must be passed from a managed object to the POJO via method call.
If a POJO will gain access to an EJB via JNDI AND the POJO is in a web
module (WAR) the reference to the EJB must be configured. Typically this
configuration will be achieved by creating EJB references in the web.xml
file. I say typically because it is possible to place an @EJB
annotation on the servlet class in place of the editing the web.xml but
using a potentially unrelated managed object class to provide
configuration for a POJO EJB reference should be avoided if possible.
Additionally if the EJB is remote it is possible to bypass the xml
configuration and use the global (non-portable, that means not using
java:comp/env) JNDI name of the EJB.
It should be noted that not all Java EE servers create default global
JNDI names for local EJBs. Access to a local EJB should only be possible
by POJOs in the same application. Same applications means that the POJO
is inside of a WAR that is inside of an EAR which also contains the ejb-jar.
Even with managed objects like Servlets annotations don't solve all your
EJB access needs. Because of the threading model of Servlets using an
annotation to gain access to a Stateful Session Bean isn't recommended
because it could easily lead to a javax.ejb.ConcurrentAccessException.
Stateless beans are ok.
Using annotations to access EJBs really works well when the client is an
EJB itself. If you follow standard design pattern practices your
EJB-based business logic has only small number of session facade beans
that are used in the web tier. For these small number of EJBs you still
might want to implement the service locater design pattern (place all
your lookup code in a utility class).
Many presentations and introductions to Java EE 5 have claimed that
annotations remove the need for JNDI lookups or xml configuration. This
is only true in some cases.
Annotations combined with dependency injection make configuration easier
and can greatly reduce the number of lines of code in a Java application
but aren't the magical bullet for every EJB client type.
-Chase
Eric Renaud wrote:
> Hello,
>
> Please review this user's comments regarding the EJB F.A.Q.
> and reply back to them.
>
> Thank you,
>
> Eric
>
> ------------------------------------------------------------------------
>
> Subject:
> [Fwd: Please update EJB FAQ on the site]
> From:
> Eric Renaud <Eric.Renaud_at_Sun.COM>
> Date:
> Wed, 11 Apr 2007 11:02:18 -0700
> To:
> ejb_at_glassfish.dev.java.net
>
> To:
> ejb_at_glassfish.dev.java.net
> CC:
> "communitymanager_at_dev.java.net" <communitymanager_at_dev.java.net>
>
>
> Hello,
>
> Please review this user's comments regarding the EJB F.A.Q.
> and reply back to them.
>
> Thank you,
>
> Eric
>
> ------------------------------------------------------------------------
>
> Subject:
> Please update EJB FAQ on the site
> From:
> Julian Klappenbach <jklappenbach_at_gmail.com>
> Date:
> Sun, 08 Apr 2007 01:53:54 -0700
> To:
> communitymanager_at_dev.java.net
>
> To:
> communitymanager_at_dev.java.net
>
>
> 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. And, what is meant by "any managed class"?
>
> 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.
>
> Please, review this FAQ, get it updated and stocked with links to
> example projects, or at least with functional fragments of code.
>
> -jjk �
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net