Menu Close

Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

Chapter 11. CDI

11.1. Overview of CDI

11.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
JBoss EAP 6 includes Weld, which is the reference implementation of JSR-299. For more information, about type-safe dependency injection, see Section 11.1.4, “About Type-safe Dependency Injection”.

11.1.3. Benefits of CDI

Following are the benefits of CDI:
  • It simplifies and shrinks your code base by replacing big chunks of code with annotations.
  • It 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 beans.xml in your META-INF/ or WEB-INF/ directory. The file can be empty.
  • It simplifies packaging and deployments and reduces the amount of XML you need to add to your deployments.
  • It provides lifecycle management via contexts. You can tie injections to requests, sessions, conversations, or custom contexts.
  • It also provides type-safe dependency injection, which is safer and easier to debug than string-based injection.
  • It decouples interceptors from beans.
  • It provides complex event notification.

11.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.

11.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 EAP 6.