Beans in CDI
Beans in 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 discuss CDI with beans. Let's begin. CDI redefines the concept of a bean beyond it's 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 it's instances may be managed by the container according to the lifecycle context model defined in the CDI specification. Basically, a bean comprises 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 may be a bean type of a bean. A bean type may be an interface, a concrete class or an abstract class and may be declared final or have final methods. A bean type may be parameterized type with type parameters and type variables.

A bean type may be an array type. Two array types are considered identical only if the element type is identical. A bean type may be 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 JavaServer Faces technology specification or to be a managed bean, it must meet some conditions like; it is not 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 ejb-jar.xml. 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 in 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.