Java Platform, Enterprise Edition (Java EE) 8 The Java EE Tutorial

## Overview of CDI

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 2.0 is specified by JSR 365. Related specifications that CDI uses include the following:

- JSR 330, Dependency Injection for Java

- The Managed Beans specification, an offshoot of the Java EE platform specification (JSR 366)

The most fundamental services provided by CDI are as follows.

- Contexts: This service enables you to bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts.

- Dependency injection: This service enables you to inject components into an application in a typesafe way and 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 web conversation scope in addition to the three standard scopes (request, session, and application) defined by the Java Servlet specification

- A complete Service Provider Interface (SPI) that allows third-party frameworks to integrate cleanly in the Java EE environment

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

  - 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

Along with loose coupling, CDI provides strong typing by

- Eliminating lookup using string-based names for wiring and correlations so that the compiler will detect typing errors

- Allowing the use of declarative Java annotations to specify everything, largely eliminating the need for XML deployment descriptors, and making it easy to provide tools that introspect the code and understand the dependency structure at development time

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.