Chapter 12. Using CDI with EJB 3

 

This chapter covers

  • Dependency injection for POJOs
  • Scopes and bean lifecycles
  • Core CDI constructs
  • Long-running conversations

Context and Dependency Injection (CDI) is an exciting new feature that was introduced with Java EE 6 and further extended in Java EE 7. CDI brings full-fledged dependency injection and context support to the Java EE platform. In earlier chapters you saw examples of resource injection using @PersistenceUnit, @Resource, and @EJB to inject resources as well as other EJBs. We’ve also delved into interceptors that provide basic AOP support. These powerful features, along with other innovations including JPA, have greatly simplified Java EE development.

But the innovations that were introduced in Java EE 5 with EJB 3 were primarily limited to EJBs. Using EJBs from the web tier required a substantial amount of boiler-plate code to retrieve instances from JNDI. JSF-backing beans were rudimentary and offered few services. Solutions such as JBoss Seam attempted to fill this gap by providing an advanced bean container that bridged the EJB and POJO world and also by providing additional services to non-EJB beans. The Java community took notice, and JBoss Seam served as the inspiration for CDI that was introduced as a core technology in Java EE 6 and expanded in Java EE 7.

12.1. Introducing CDI

12.2. CDI beans

12.3. Next generation of dependency injection

12.4. Interceptor and decorators

12.5. Component stereotypes

12.6. Injecting events

12.7. Using conversations

12.8. Using CDI effectively with EJB 3

12.9. Summary