Search code examples
jakarta-eecdiioc-container

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?


Solution

  • 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.