Java Reference
In-Depth Information
Contexts and Dependency Injection
The Contexts and Dependency Injection(CDI) for Java EE 1.1 Specification was de-
veloped under JSR 346. This section just gives you an overview of improvements in
the API. The complete document specification (for more information) can be down-
loaded from .
What is CDI ?
Introduced in the Java EE platform from Version 6, Contexts and Dependency Injec-
tion for Java EE (CDI) is a specification that has brought to the platform a set of ser-
vices that simplify the management of the lifecycle of objects, and standardize and
encourage the use of dependency injection in the Java EE environment. In concrete
terms, this specification gives us the ability to easily link in a loosely coupled and type-
safe way the different layers (presentation, business and data access) of n-tier archi-
tectures. In order to do this, the CDI primarily relies on two services that are:
Context : This is used for the management of the lifecycle of the objects (the
time of creation and destruction) based on their scope.
Dependency Injection : This includes a number of elements such as the in-
jection of a component into another, the choice of implementation to be injec-
ted for a given interface and the type of object provided to access the injected
dependence: a proxy or a reference that gives direct access to the instance.
To get a better idea of the power of CDI, let us take some examples.
Example 1 - instantiation of a POJO
Suppose we have a JSF managed bean that wants to access an instance of a POJO
that implements an interface. The basic approach is to create an instance of the
POJO in the managed bean by using the new keyword as is the case in the following
public class MyManagedBean {
IHelloWorld hw = new HelloWorld();
Search WWH ::

Custom Search