Language and Page Formatting Options
Chapter 8. CDI
8.1. Overview of CDI
8.1.1. Overview of CDI
8.1.2. About Contexts and Dependency Injection (CDI)
Contexts and Dependency Injection (CDI) is a specification designed to enable EJB 3.0 components "to be used as Java Server Faces (JSF) managed beans, unifying the two component models and enabling a considerable simplification to the programming model for web-based applications in Java." The preceding quote is taken from the JSR-299 specification, which can be found at http://www.jcp.org/en/jsr/detail?id=299.
JBoss Enterprise Application Platform includes Weld, which is the reference implementation of JSR-299. For more information, about type-safe dependency injection, see Section 8.1.4, “About Type-safe Dependency Injection”.
8.1.3. Benefits of CDI
- CDI simplifies and shrinks your code base by replacing big chunks of code with annotations.
- CDI is flexible, allowing you to disable and enable injections and events, use alternative beans, and inject non-CDI objects easily.
- It is easy to use your old code with CDI. You only need to include a
WEB-INF/directory. The file can be empty.
- CDI simplifies packaging and deployments and reduces the amount of XML you need to add to your deployments.
- CDI provides lifecycle management via contexts. You can tie injections to requests, sessions, conversations, or custom contexts.
- CDI provides type-safe dependency injection, which is safer and easier to debug than string-based injection.
- CDI decouples interceptors from beans.
- CDI provides complex event notification.
8.1.4. About Type-safe Dependency Injection
Before JSR-299 and CDI, the only way to inject dependencies in Java was to use strings. This was prone to errors. CDI introduces the ability to inject dependencies in a type-safe way.
For more information about CDI, refer to Section 8.1.2, “About Contexts and Dependency Injection (CDI)”.
8.1.5. Relationship Between Weld, Seam 2, and JavaServer Faces
The goal of Seam 2 was to unify Enterprise Java Beans (EJBs) and JavaServer Faces (JSF) managed beans.
JavaServer Faces (JSF) implements JSR-314. It is an API for building server-side user interfaces. JBoss Web Framework Kit includes RichFaces, which is an implementation of JavaServer Faces and AJAX.
Weld is the reference implementation of Contexts and Dependency Injection (CDI), which is defined in JSR-299. Weld was inspired by Seam 2 and other dependency injection frameworks. Weld is included in JBoss Enterprise Application Platform.