Beans in CDI


Context and Dependency Injection
What is CDI?
Beans in CDI

The course is part of this learning path

Beans in 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 discuss CDI with beans. Let's begin. CDI redefines the concept of a bean beyond its use in other Java technologies such as the java beans and enterprise Java beans as EJB technologies. In CDI, a bean is a source of contextual objects that define application state and logic. A Java EE component is a bean. If the lifecycle of its instances may be managed by the container according to the lifecycle context model defined in the CDI specification. Basically, a bean comprises of the following attributes. 

A nonempty set of bean types; a nonempty set of qualifiers; a scope; optionally, a bean EL name; a set of interceptor bindings; a bean implementation. A bean type defines a client visible type of the bean. Almost any Java type maybe a bean type of a bean. A bean type maybe an interface, a concrete class or an abstract class and maybe declared final or have final methods. A bean type maybe a parameterized type with type parameters and type variables.

A bean type maybe an array type. Two array types are considered identical only if the element type is identical. A bean type maybe a primitive type, primitive types are considered to be identical to their corresponding wrapper types in Java.lang. Beans are components that can be injected into each other with a dependency injection. Almost every plain old java object as POJO with a constructor can be a bean. In CDI, a bean is a source of contextual objects that define application state and logic. 

The beans are managed by CDI containers by using bean defining annotations. The container instantiates dependencies and controls when and how the components are instantiated and destroyed. Annotations are metadata about code. You can use them to decorate a class, method, field, parameter variable constructor or package. You can also use annotations to define the scope in which the bean is stored. A managed bean is implemented by a Java class, which is called it's bean class.

A top level Java class is a managed bean if it is defined to be a managed bean by any other Java EE technology specification, such as the Java server faces technology specification. Or to be a managed bean, it must meet some conditions like; it is a non-static inner class, it is a concrete class or is annotated decorator. It is not annotated with an EJB component defining annotation or declared as an EJB bean class in an EJBjar.XML file. No special declaration such as an annotation is required to define a managed bean. 

It has an appropriate constructor. This means the class has a constructor with no parameters or the class declares a constructor annotated inject. The concept of injection has been part of Java technology for some time. Since the Java EE 5 platform was introduced, annotations have made it possible to inject resources and some other kinds of objects into container managed objects. CDI makes it possible to inject more kinds of objects and to inject them into objects that are not container managed.

Let's look at the injectable objects. Almost any Java class, session beans, java EE resources such as data sources, java message service topics, queues and connection factories, persistence contexts, JPA entity manager objects, producer fields, objects returned by producer methods. Web service references, remote enterprise bean references. For example, suppose that you create a simple java class with a method that returns a string, this class becomes a bean that you can then inject into another class. This bean is not exposed to the EL in this form. Giving beans EL names explains how you can make a bean accessible to the EL. That's all I have for now. So, let's take a quick break here. In the next video, we will cover qualifiers. 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