Java Reference
In-Depth Information
override the annotations for an EJB . You could, for example, use a deployment
descriptor to map an entity bean to a different database schema. As of this writing,
EJB 3 deployment descriptors are still a work in progress and so we won't discuss
them further.
Dependency injection
An EJB rarely works in isolation. It typically uses resources such as JDBC Data-
Source s and even other EJB s to fulfill its responsibilities. An EJB 2 bean uses JNDI
to look up these resources and EJB s. The problem with using JNDI is that in addi-
tion to requiring you to write the lookup code it couples the EJB to the applica-
tion server environment. EJB 3 fixes this problem for session and message-driven
beans by using dependency injection to encapsulate the JNDI lookup. It defines
several annotations that you can use to identify a field or setter method as requir-
ing a reference to an EJB or resource. When the EJB container instantiates a ses-
sion or message-driven bean, it initializes the fields and calls the setters with
objects obtained from JNDI . For example, here is how the PlaceOrderFacadeImpl
EJB can use field injection to obtain a reference to the PlaceOrderService EJB :
@Stateless
class PlaceOrderFacadeImpl implements PlaceOrderFacade {
@EJB
PlaceOrderService service;
The @EJB annotation tells the EJB container to set the service field to a reference
to the PlaceOrderService , which is another EJB deployed in the container. By
default, EJB 3 derives the JNDI name from the type of the field or setter parameter,
but if necessary it can be specified in the annotation.
EJB 3 dependency injection is extremely useful. By eliminating the JNDI look-
ups, it simplifies session and message-driven beans and reduces their dependency
on the application server environment. It is also quite concise, unlike Spring
dependency injection, which requires you to write XML to configure the beans.
There are, however, some limitations.
Significantly improved O/R mapping
Historically, the EJB CMP , which is the EJB equivalent of persistent objects, has
been very weak. For example, EJB 1 CMP did not support relationships and it
wasn't until EJB CMP 2.1 that the query language supported sorting! EJB 3 persis-
tence is a huge improvement over EJB 2 CMP . It has many features that were lack-
ing from EJB 2 , including the following:
 
 
 
 
Search WWH ::




Custom Search