25

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 ?

CodeSlave
  • 425
  • 6
  • 21
Yashar
  • 1,122
  • 2
  • 15
  • 43
  • did you read some manuals/faq about it? In what context are you using it? Did you read http://docs.oracle.com/javaee/6/tutorial/doc/giwhl.html ? Are you even aware that this is java-ee because I retagged it. – El Hocko Mar 20 '13 at 10:07
  • I am talking about Jboss Weld – Yashar Mar 20 '13 at 10:48

5 Answers5

15

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!

Mohan
  • 4,755
  • 2
  • 27
  • 20
7

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 .

CodeSlave
  • 425
  • 6
  • 21
user2466922
  • 71
  • 1
  • 2
  • 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). – JL_SO Aug 30 '20 at 18:31
6

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.

CodeSlave
  • 425
  • 6
  • 21
Michail Nikolaev
  • 3,733
  • 22
  • 18
  • 1
    what are injected and injects annotation, are you using At-sign as a replacement for quotation here? – pref Jun 08 '22 at 15:33
3

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.
CodeSlave
  • 425
  • 6
  • 21
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
2

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();
Marinos An
  • 9,481
  • 6
  • 63
  • 96