Dependency Injection in JBoss 4.2: Hold Your Excitement

For the last little while I have been pushing my company to migrate our Enterprise Beans to EJB3 (most of our beans are of the 2.1 variety) and Dependency Injection (DI) has been one of the EJB3 features that I have been very excited to see getting used throughout our middle tier.

DI is a great new addition to the EJB spec which gives the ability to ‘inject’ an EJB reference in another bean simply by using the @EJB annotation:

MyBeanInterface myBeanReference;

At run-time, the container populates the field with the appropriate bean reference, thus saving the developer some manual JNDI lookup code. For a more thorough discussion of DI, check out Debu Panda’s article.

The biggest selling point for us was that EJB references can also be injected with a bean setter methods, e.g.

void setMyBean(MyBeanInterface myBean){
this.myBean = myBean;

which can make a bean easily unit-testable with the setter used to pass in a mock object during the test set up.

However when we started using DI with the latest stable version of JBoss (4.2.2) we found that DI of EJB references has limited support. Namely it is only supported for objects inside the EJB container. This means that while beans can refer to each other with the @EJB annotation, other managed objects like servlets and non-EJB web services must still use JNDI lookup to access any Enterprise Beans.

To quote from the JBoss docs:

“JBoss Application Server 4.2.2 implemented EJB3 functionality by way of an EJB MBean container running as a plugin in the JBoss Application Server. This had certain implications for application development. The EJB3 plugin injects references to an EntityManager and @EJB references from one EJB object to another. However this support is limited to the EJB3 MBean and the JAR files it manages. Any JAR files which are loaded from a WAR (such as Servlets, JSF backing beans, and so forth) do not undergo this processing”

So even though the JEE 5 specification stipulates a wider scope of the @EJB annotation, JBoss 4.2.2 doesn’t support it. In fact JBoss 4.2.2 is a ‘bridge’ version that doesn’t claim full JEE 5 compliance. And while I am grateful for the many other JEE 5 features JBoss currently does provide, I hope this post saves people out there the time and effort of trying in vain to make @EJB references work for servlets, web services or other WAR components.


13 comments so far

  1. Jim Boone on


    Thank you for the timely post!! I have been pulling my hair out trying to inject an EJB into a ServletContextListener with no luck at all! This was my first venture into EJB3 and I thought I was doing something wrong. I am vindicated! Thanks again.

  2. bhaskar on

    Using Spring 2.5’s Annotation based DI, you can inject a EJB in almost any class, that is managed by Spring.
    I am using this right now and it is very easy to do.

  3. Olivier on

    Thank you!

  4. Serge on

    Hi, thanks for this post that confirm my own experience.
    I’m looking for a good simple example of Spring 2.5’s Annotation based DI for EJB.
    Any help?

  5. Joe on

    I found the same problem after a lot of hair pulling why my servlets gave nullpointerexceptions when using dependency injection.

    A solution could be to use JSF in combination with JBoss Seam.

  6. maxima on

    Thanks a lot.

  7. venkat on

    Yes you are right. The dependency injection does not work from Servlets for example. And as the link ( says, I could make the general JNDI work as follows..

    InitialContext ctx = new InitialContext();
    Converter converter = (Converter)ctx.lookup(“”);

  8. venkat on

    Another post on says about the same problem:

  9. David Wallace Croft on

    Thanks for the info. I have been struggling with trying to get it working for over an hour. Nice to know that it is not working because it is not supported. I sure wish the JBoss documentation would have included this in big bold letters.

  10. Vijay Mohan P on

    Thank u very much pal.

    i was trying for this last 3 days.

    now i got the idea.

  11. Pavel on

    Really thanks!!! You save me after an hour of crazy trying.

  12. don on

    Why use ejbs? use spring DI instead ( with annotated security/transactions)

  13. Stas Ostapenko on

    Thanks for this post ! Now I see why I’m facing NPE’s

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s