The Benefits and Limitations of Dependency Injection
Dependency injection makes resources and services easier to use because you don't have to deal with the complexities of JNDI. You don't have to write hundreds of lines of code or write service locator patterns.
Java EE supports dependency injection either via XML or annotation, so use annotation only when it makes sense. Annotation can make your code more readable and concise--not to mention make your life easier--but it can also cause maintenance problems, since the method requires that resource references are hard-wired into the application code. The good news is that you can override annotations using XML elements.
Because Java EE 5.0 supports dependency injection with managed classes, you can't use dependency injection from helper classes. And you must still use JNDI if you want to use resources or services from a helper class.
Dependency injection will greatly simplify the complexities of JNDI and make it easier to develop enterprise applications. See for yourself how it makes using resources and services easier: try it out in Oracle Application Server 10g 10.1.3 and JBoss 4.0.x, both of which offer early support for EJB 3.0 and dependency injection in the EJB container.
- OTN EJB 3.0 Resource Center
- Java EE 5.0 Early Release draft specification (JSR 244)
- Common Metadata annotations for Java Platform Proposed Final Draft specification (JSR 250)
- EJB 3.0 Public Draft specification (JSR 220)
- Java API for XML web services 2.0 Proposed Final Draft specification (JSR 224)
Debu Panda is a Senior Principal Product Manager of the Oracle Application Server development team.
Return to ONJava.com.