Context and Dependency Injection within Java EE


Context and Dependency Injection
What is CDI?
Beans in CDI

The course is part of this learning path

What is CDI?

This course explores Context and Dependency Injection (CDI) within Java EE. We'll look at what it is, as well as beans in CDI and CDI qualifiers.

Learning Objectives

  • Learn the fundamentals of CDI
  • Learn about beans and qualifiers

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.


Basic knowledge of Java programming.


Hello dear friends. In this video, we will look to answer the question, what is CDI?

Let's begin. CDI defines a type safe dependency injection mechanism in the Java EE platform. A bean specifies only the type and semantics of other beans it depends upon without a string name and using the type information available in the Java object model. This allows compile time validation in addition to deployment. It also provides for easy refactoring. The injection request need not be aware of the actual lifecycle, concrete implementation, threading model or other clients of the bean. This strong typing, loose coupling, makes your code easier to maintain. The beans so injected has a well defined lifecycle and is bound to lifecycle contexts. The injected bean is also called a contextual instance because it is always injected in a context. Almost any POJO can be injected as a CDI bean.

This includes EJBs, JNDI resources, entity classes and persistence units in contexts. Even the objects that were earlier created by a factory method can now be easily injected. Specifically, CDI allows EJB components to be used as JSF-managed beans, thus, bridging the gap between the transactional and the web tier. It is also integrated with unified expression language as UEL allowing any contextual object to be used directly with a JSF or JSP page. The most fundamental services provided by CDI are contexts and dependency injection. Context is the ability to bind the lifecycle and interactions of state components to well defined but extensible lifecycle contexts.

Dependency injection is the ability to inject components into an application in a type safe 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 Java Server Faces page or a Java Server Pages page, the ability to decorate injected components, the ability to associate interceptors with components using type safe interceptor bindings, an event-notification model, a web conversion scope in addition to the three standard scopes as request, session and application defined by the Java Servlet specification, a complete service provider interface as SPI that allows third party frameworks to integrate cleanly in the Java EE6 environment. A major theme of CDI is loose coupling. CDI decouples the server and the client by means of well defined types and qualifiers so that the server implementation may vary and decouples the lifecycles of collaborating components.

But how? Makes components contextual with automatic lifecycle management and allows 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. That's all I have for now. In the next video, we will examine beans in CDI. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics