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 re-
source 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 simpli-
fied 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 re-
trieve 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 con-
tainer 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.
This chapter will cover all of the fundamental concepts of CDI, as well as the basic con-
structs. Throughout this chapter you'll use CDI to build a robust front end to ActionBazaar
and glue the JSF presentation layer to the EJBs. Let's start by looking at the genesis of CDI
and the technologies that influenced its evolution.
12.1. Introducing CDI
CDI was originally developed via the Java Community Process (JCP) as JSR-299. Origin-
ally JSR-299 was called WebBeans, but it quickly became evident that the features encom-
passed within JSR-299 went far beyond replacing the oft-maligned JSF-managed beans. The