What is a CDI bean?

I'm a little confused, we call the CDI bean in beans, which we insert them using the @inject annotation or beans, which we use inside them inside @?

+8
java java-ee cdi
source share
4 answers

A CDI bean is a bean managed by a CDI container (such as Weld). So, if it is @Injected - it is a bean, if it can be @ integrate something - it is also a bean.

+2
source share

CDI does not introduce a new type of bean called the "CDI Bean" with its own unique component model. CDI provides a set of services that can be used by managed beans and EJBs, which are defined by their existing component models. So CDI is just a bean (EJB or Managed Bean) that manages the CDI life cycle with scope for Context and another old DI feature.

+4
source share

CDI appeared in Java EE 6 to provide some of the previously available EJB features for all container-managed components only. Thus, the CDI bean covers servlets, SOAP web service, RESTful web service, objects, EJB, etc.

So you can use all these interchagebly terms: CDI bean, bean managed by bean, EJB bean managed by container bean, etc.

+2
source share

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

For example, PrintServlet gets a dependency on the Message instance and automatically enters it using 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!"; } } 

Hooray!

+1
source share

All Articles