What is a CDI bean?

29,771

Solution 1

CDI bean is a bean managed by CDI container (Weld for example).

  • If it is @injected - it is bean

  • If it is may @injects something - it is bean too.

Solution 2

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!

Solution 3

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 .

Solution 4

CDI got introduced in Java EE 6 to provide some of the features available earlier to EJB only to all components managed by container. So CDI bean covers Servlets, SOAP web service, RESTful web service, entities, EJBs etc.

So you can use all these terms interchagebly :

  • CDI bean
  • bean
  • managed bean
  • EJB bean
  • container managed bean etc.

Solution 5

I think the term CDI bean might not be technically correct. Managed bean seems more appropriate.

Though, a possible definition of CDI bean could be: Any managed bean that was created and injected as a result of the presence of CDI annotations on a class or as a result of the prensence of beans.xml file inside the archive. A CDI bean itself is not a class but a managed instance of a class.

e.g. take the example below:

public class Logger{}
public class Producer {

    @Produces
    public Logger getLogger() {
        return new Logger();
    }
}

Logger itself is just a class. It becomes a managed bean (CDI bean) after it is instantiated by the producer and is bound to a context.

CDI beans can only exist inside a container. The container needs to implement the CDI specification. It scans part or all of the classpath for CDI annotations based on which it creates and manages the corresponding beans. Creating a CDI-enabled container from a standalone application is as easy as this:

WeldContainer container = new Weld().initialize();
Share:
29,771
Yashar
Author by

Yashar

Updated on March 24, 2020

Comments

  • Yashar
    Yashar about 4 years

    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 ?

    • El Hocko
      El Hocko about 11 years
      did you read some manuals/faq about it? In what context are you using it? Did you read docs.oracle.com/javaee/6/tutorial/doc/giwhl.html ? Are you even aware that this is java-ee because I retagged it.
    • Yashar
      Yashar about 11 years
      I am talking about Jboss Weld
  • JL_SO
    JL_SO over 3 years
    I don't think this is true - afaik there is such a thing as a CDI bean, which is distinct from a fully fledged EJB bean. A CDI bean for example doesn't automatically get transactionalised (this is something that an EJB gets).
  • pref
    pref almost 2 years
    what are injected and injects annotation, are you using At-sign as a replacement for quotation here?