November 30th, 2009
5:28 pm
Posted under Glassfish
Permalink
Tags EJB, Glassfish, JNDI, Tip
This Glassfish article is a must read reference on the naming and JNDI minefield when looking up local and remote EJBs.
There is also a very informative Refcardz card on this topic. Refcardz can be downloaded as PDFs if you register.
In my experience, there is a lot of misinformation around the internet on this which can be time consuming to weed out.
I’ve posted some examples of local and remote EJB access via both injection and JNDI here.
Here are some key points I worked through and learned :-
- Despite a number of blog posts to the contrary, EJBs can be injected via @EJB into JSF managed beans, POJOs called from managed beans, and servlets provided the JSF/servlets are running in the EJB container, e.g. if Glassfish is used both as servlet and EJB container.
- When injecting local EJBs, using the defaults on @EJB and matching defaults e.g. on @Stateless will work and the bean will inject.
- When injecting remote EJBs via @EJB, use the mappedName argument to specify the JNDI name of the remote bean. It is also essential to specify the class of the remote EJB interface in the beanInterface argument to @EJB. Failure to do this will result in NamingExceptions, which can give the misleading impression that it is the JNDI name that is wrong.
- If @EJB is not used, then <ejb-local-ref> may be used in web.xml to define a reference to the bean. If @EJB is used then this is handled automatically. The above Glassfish article details how the various fields in @EJB and <ejb-local-ref> are related.
- In addition to @EJB and direct JNDI lookups, EJBs can be injected via JSF using a custom EL resolver which performs the JNDI lookups and injections performed in faces-config.xml. An interesting example of a custom EL resolver to do this is here. However, for injecting EJBs, @EJB requires no extra code. It is also possible to inject EJBs via Spring, into beans managed by a Spring container. However these have not been pursued as @EJB is the easiest way. It would be overkill to use a Spring container purely for EJB injection/service location.
Leave a Reply
You must be logged in to post a comment.