Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does CDI container fit in EJB/Web container of Java EE server?

A Java EE server has different containers like EJB or Web Container: enter image description here

I found however different information on how the CDI component is integrated. In the litarature some speak of a CDI container ("The CDI container manages all beans inside the scope automatically for you") but others define it as contextual services provided by Java EE containers. Here on stackoverflow there is even a tag called "IOC-Container". So if it is a container is that container part/inside of the EJB/Web container?

So if I draw a picture of a Java EE server with its components and containers (like the above illustration) how does CDI fits in there? Does it get its own container "rectangle" or is it part of some EJB/Web container? How would you draw it in an architectural design and how would you explain/describe it?

like image 770
Lonzak Avatar asked Dec 07 '16 09:12

Lonzak


People also ask

Which Java EE technologies can be used in web and EJB containers?

A Java EE server provides EJB and web containers. Enterprise JavaBeans (EJB) container: Manages the execution of enterprise beans for Java EE applications. Enterprise beans and their container run on the Java EE server.

What is CDI and EJB?

They are CDI, EJB3 and JSF Managed Beans. CDI is the new kid on the block. CDI beans feature dependency injection , scoping and an event bus . CDI beans are the most flexible with respect to injection and scoping. The event bus is very lightweight and very well suited for even the simplest of web applications.

What is the difference between Web Container EJB container and application server?

The main difference between the web containers and application server is that most web containers such as Apache Tomcat implements only basic JSR like Servlet, JSP, JSTL wheres Application servers implements the entire Java EE Specification.

What is meant by Web container in Java EE?

Web container: Manages the execution of JSP page and servlet components for Java EE applications. Web components and their. container run on the Java EE server. Application client container: Manages the execution of application client components. Application clients and their container run on the client.


1 Answers

CDI has a separate container. In your picture it would be most likely another separate rectangle on EE server side. Talking about EE environment, CDI is provided by the EE container (usually the RI - Weld) and as such the container will start it for you upon (first) application deployment.

That being said, CDI container is one per EE server. Even if you deploy several WAR applications all of which use CDI, there will still be one CDI container. Note that in SE environment (Weld SE allows you to use CDI in SE env.), you can have several such containers if you so wish.

As for integration with other EE stuff like EJB and JSF. They for instance allow you to use @Stateless or @ViewScoped which are CDI based features. So they have to integrate with CDI "somehow". Now this magic is done inside the EE container, usually via provider's (Weld) SPI. For instance the container is responsible for handling EJB beans and then handing them over (through SPI) to Weld which can then take them over as CDI beans allowing you to @Inject into @Stateless beans. Obviously the above is simplification of what actually happens.

like image 56
Siliarus Avatar answered Oct 07 '22 11:10

Siliarus