Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Resource Injection in the Java EE platform Overview

Resource Injection in the Java EE platform Overview

One of the simplification features of Java EE 5 is the implementation of basic dependency injection to simplify web and EJB components. Dependency injection, called resource injection by the spec designers uses annotations or XML descriptors to declare resources or environment entries used by a class, eliminating JNDI lookups. Life-cycle hooks can also be declared with annotations.   A new tutorial on gives a quick overview of how to use annotations to do resource injection and simplify access to resource and environment data injection, as well as life-cycle control.   Another good source for what's new in EE 5 is the introduction to the Java EE platform article.

Java EE 5 can handle injections transparently when used on container managed components.  From the Java EE 5 spec, the following Java EE components support injection and the post/pre construct annotations:

Typically injection will occur after an instance of the class is constructed, but before any business methods are called. Method injection is also supported.

Injection is limited only to first class constructs defined in the Java EE platform, including:
  • SessionContext object
  • DataSources object
  • UserTransaction
  • EntityManager interface
  • TimerService interface
  • Other enterprise beans
  • Web services
  • Message queues and topics
  • Connection factories for resource adaptes
  • Environment entries limited to String, Character, Byte, Short, Integer, Long, Boolean, Double, and Float.
The injection facilities in Java EE 5 do not apply to any POJO (which is often criticized by the Spring community) or two JSPs, Tag files, and certain JSF constructs:
Supporting resource injection on JavaServer Faces technology renderers, converters, or validators is not desirable because that would violate Model-View-Controller (MVC) separation of these presentation objects.  If these classes need to get access to resources, they should do so through a managed bean
It is interesting to note that the Java standards emphasize Annotation-driven injection, while the Spring community which popularized dependency injection continues to ignore annotations as a means of injection, using XML descriptors instead.

Rate this Article