Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a CDI bean?

Tags:

I am a little bit confused, we call CDI bean to the beans which we inject them using @Inject annotation or the beans which we use @Inject inside them ?

like image 345
Yashar Avatar asked Mar 20 '13 10:03

Yashar


People also ask

What is CDI in spring?

CDI stands for "context and dependency injection", while Spring is a complete ecosystem around a dependency injection container.

What is Weld CDI?

Weld is the reference implementation of CDI: Contexts and Dependency Injection for the Java EE Platform - a JCP standard for dependency injection and contextual lifecycle management and one of the most important and popular parts of the Java EE.

What is CDI in JSF?

Contexts and Dependency Injection (CDI), specified by JSR-299, is an integral part of Java EE 6 and provides an architecture that allows Java EE components such as servlets, enterprise beans, and JavaBeans to exist within the lifecycle of an application with well-defined scopes.


2 Answers

CDI beans are classes that CDI can instantiate, manage, and inject automatically to satisfy the dependencies of other objects. Almost any Java class can be managed and injected by CDI.

For example, PrintServlet got dependency on a Message instance and have it injected automatically by the CDI runtime.

PrintServlet.java

@WebServlet("/printservlet") public class PrintServlet extends HttpServlet {     @Inject private Message message;      @Override     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {         response.getWriter().print(message.get());     } } 

Message.java (This class is a CDI bean)

@RequestScoped public class Message {     @Override     public String get() {         return "Hello World!";     } } 

Cheers!

like image 50
Mohan Avatar answered Oct 24 '22 08:10

Mohan


CDI does not introduce a new bean type called a CDI Bean with its own unique component model.

CDI provides a set of services that can be consumed by managed beans and EJBs that are defined by their existing component models.

So, CDI is just a Bean (EJB or Managed Bean) handling CDI lifecycle with scope for Context and other old feature DI .

like image 43
user2466922 Avatar answered Oct 24 '22 08:10

user2466922