With EJB 3, dependency injection has greatly simplified accessing both EJB resources"such as JDBC DataSource, JMS Objects, and JPA Entity Manager"and services"such as Timer, User Transaction, and Web Services. You will find this Refcard useful when building enterprise Java applications with EJB 3 and JPA. It lists all metadata annotations, describes them and provides examples. It also provides descriptions for XML elements that you can use for injection.
Most enterprise Java applications use external resources and services such as Data Source, EJB, or web services. EJB 3 makes using resources and services simpler by implementing dependency injection.
Dependency injection allows you to simply declare component dependencies and let the EJB container deal with the complexities of instantiating, initializing, and sequencing resources and supplying service or resource references to clients as required. Development frameworks like Spring framework originally popularized dependency injection.
In EJB 3, you may think of dependency injection as the inverse of JNDI. It is the responsibility of the container to inject an object based on the dependency declaration. The figure below compares dependency injection with JNDI.
Note that annotations and descriptors are not mutually exclusive. In fact, you can use both together. Deployment descriptor entries override configuration values specified using metadata annotations.
You can use either metadata annotations or XML descriptors to use dependency injection.
Refer to the following Java EE 5 specifications and XML schema for more information on dependency injection.
JSR 220
Enterprise JavaBeans 3.0:
http://www.jcp.org/en/jsr/detail?id=220
JSR 224
Java API for XML-Based Web Services (JAX-WS) 2.0:
http://jcp.org/en/jsr/detail?id=224
JSR 250:
Common Annotations for the Java Platform:
http://jcp.org/en/jsr/detail?id=224
Schema for EJB 3 deployment descriptor:
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
Schema that defines common schema components:
http://java.sun.com/xml/ns/javaee/javaee_5.xsd
Dependency injection is supported only on managed classes and not on regular POJO.
Dependency injection is supported only on managed classes such as Session Beans or Interceptors and not on regular POJO. Hence you cannot use injection on helper classes.
The following table depicts what type of managed classes can inject what objects.
Resources |
Stateless |
Stateful |
MDB |
Interceptors |
JDBC DataSource |
Yes |
Yes |
Yes |
Yes |
JMS Destinations, Connection Factories |
Yes |
Yes |
Yes |
Yes |
Mail Resources |
Yes |
Yes |
Yes |
Yes |
UserTransaction |
Yes |
Yes |
Yes |
Yes |
Environment Entries |
Yes |
Yes |
Yes |
Yes |
EJBContext |
Yes |
Yes |
Yes |
No |
Timer Service |
Yes |
No |
Yes |
No |
Web Service reference |
Yes |
Yes |
Yes |
Yes |
EntityManager, EntityManagerFactory |
Yes |
Yes |
Yes |
Yes |
Java EE 5 introduced several metadata annotations as part of JSR 250. Although primarily geared toward EJB, these annotations also apply to Java EE components such as Servlets, JSF managed beans, and application clients.
The following annotations can be used in a field or a setter method for dependency injection. However, these annotations may also be applied at the class level for defining dependencies and then used with JNDI look up.
Annotations |
Usage |
Components that can use |
javax.annotation.Resource |
Dependency injection of resources such as DataSource, and JMS objects |
EJB, Web, Application Client |
javax.ejb.EJB |
Dependency injection of Session beans |
EJB, Web, Application Client |
javax.xml.ws.WebServiceRef |
Dependency injection of Web services |
EJB, Web, Application Client |
javax.persistence.PersistenceContext |
Dependency injection of container-managed EntityManager |
EJB, Web |
javax.persistence.PersistenceUnit |
Dependency injection of EntityManagerFactory |
EJB, Web |
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}