Understanding CDI / Weld in a multi-module application

I have an application that is packaged in an EAR containing many JARs (with EJBs, libraries, third-party libraries ...) and one WAR (again containing some other JARs). The application is deployed in a JEE7 container (Wildfly 8.0.0.Final) and uses CDI (Weld 2.1.2.Final comes with Wildfly).

In my understanding, Weld is active throughout the application and has a single application. Therefore, it does not matter where I want to use CDI - it works.

But there are some indications that suggest that this is not true. For example. toString method of BeanManager shows different output in different modules:

When using BeanManager in some module that is packaged in a war, I get Weld BeanManager for test-ear-1.0-SNAPSHOT.ear/test-webui-frontend-1.0-SNAPSHOT.war/WEB-INF/lib/test-webui-backend-1.0-SNAPSHOT.jar [bean count=76] .

If it is used in the library directly contained in the EAR: Weld BeanManager for test-ear-1.0-SNAPSHOT.ear/test-ejb3-dao-1.0-SNAPSHOT.jar/ [bean count=224] .

So, it seems that these BeanManagers โ€œresponsibleโ€ for different parts of the application. It's true?

+7
java cdi weld
source share
1 answer

This is rather handled by an application server that understands the EAR specification.

From Wikipedia:

Most application servers load classes from the deployed EAR file as an isolated tree of Java class loaders, isolating the application from other applications, but separating classes between the deployed modules. For example, a deployed WAR file will be able to instantiate classes defined in a JAR file, which was also included in the containing EAR, but not necessarily in JAR files in other EAR files. One of the main reasons for this behavior is the complete separation between applications using static singletones (for example, Log4J), which otherwise confuse the configuration between separate Applications. It also allows you to use different versions of applications and libraries that will be deployed side by side.

Thus, each module has its own BeanManager , and the application server manages the dependencies between these instances.

+4
source share

All Articles