Contexts and Dependency Injection (CDI) is one of several Java EE 6 features that help to knit together the web tier and the transactional tier of the Java EE platform.
CDI is a set of services that, used together, make it easy for developers to use enterprise beans along with JavaServer Faces technology in web applications. Designed for use with stateful objects, CDI also has many broader uses, allowing developers a great deal of flexibility to integrate various kinds of components in a loosely coupled but typesafe way.
CDI is the Java standard for dependency injection (DI) and interception (AOP).
The most fundamental services provided by CDI are as follows:
- Contexts: The ability to bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts
- Dependency injection: The ability to inject components into an application in a typesafe way, including the ability to choose at deployment time which implementation of a particular interface to inject
In addition, CDI provides the following services:
- Integration with the Expression Language (EL), which allows any component to be used directly within a JavaServer Faces page or a JavaServer Pages page
- The ability to decorate injected components
- The ability to associate interceptors with components using typesafe interceptor bindings
- An event-notification model
A major theme of CDI is loose coupling. CDI does the following:
- Decouples the server and the client by means of well-defined types and qualifiers, so that the server implementation may vary
- Decouples the lifecycles of collaborating components by doing the following:
- Making components contextual, with automatic lifecycle management
- Allowing stateful components to interact like services, purely by message passing
- Completely decouples message producers from consumers, by means of events
- Decouples orthogonal concerns by means of Java EE interceptors