Contexts and Dependency Injection


What is CDI?
Beans in CDI

The course is part of this learning path

What is CDI?

In this course, we will learn the concepts of microservice and spring framework with a focus on CDI.

Learning Objectives

  • Understand contexts and dependency injection (CDI)

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

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 requests 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 bean so injected has a well-defined lifecycle and is bound to lifecycle context. 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 and context. 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 within 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 stateful 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 conversation 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 EE 6 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? CDI makes components contextual with automatic lifecycle management and allows stateful components to interact like services purely by message passing. CDI completely decouples message producers from consumers by means of events. CDI decouples orthogonal concerns by means of Java EE interceptors. Along with loose coupling, CDI provides strong typing by eliminating look up using string-based names for wiring and correlations, so that the compiler will detect typing errors allowing the use of declared of 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. Okay, that's all I have for now. In the next video, we will examine beans and CDI. So, I'll 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.