concept @EJB in category ejb

appears as: @EJB, @EJB
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

Java EE 5 had a basic form of dependency injection that EJB could use. It was called resource injection and allowed you to inject container resources, such as data sources, queues, JPA resources, and EJBs, using annotations like @EJB, @Resource, and @PersistenceContext. These resources could be injected into Servlets, JSF backing beans, and EJB. The problem was that this was very limiting. You weren’t able to inject EJB into Struts or JUnit, and you couldn’t inject non-EJB DAOs (data access objects) or helper classes into EJB.

With Java EE 5, the @EJB annotation was added to the specification to make it easier for container-managed components to get reference to EJBs. Following the inversion of control best practice, the container uses the DI pattern to wire up beans. Although JNDI is still used in the background, those details are hidden from the developer by the @EJB annotation. Prior to EE5, JNDI was used exclusively. The key difference is it was the developer’s responsibility to wire up beans manually using a JNDI implementation of the service locator pattern instead of having the container do it through dependency injection. In this section we’ll first review the basics of JNDI to gain an understanding of what’s happening behind the scenes. Then we’ll look at the @EJB annotation in more detail, demonstrating how and when to use the annotation and how it may also be used in conjunction with JNDI.