java - 了解多模块应用中的 CDI/Weld

标签 java cdi weld

我有一个打包在 EAR 中的应用程序,其中包含许多 JAR(带有 EJB、库、第 3 方库……)和一个 WAR(同样包含一些其他 JAR)。该应用程序部署在 JEE7 容器 (Wildfly 8.0.0.Final) 中并使用 CDI(Weld 2.1.2.Final 随 Wildfly 一起提供)。

据我了解,Weld 在应用程序范围内处于 Activity 状态,并且具有单一的应用程序范围 View 。因此,我想在何处使用 CDI 并不重要 - 它可以正常工作。

但有一些迹象表明这不是真的。例如。 toString - BeanManager 的方法在不同的模块中显示不同的输出:

在 war 中打包的某个模块中使用 BeanManager 时,我得到 为 test-ear-1.0-SNAPSHOT.ear/test-webui-frontend-1.0-SNAPSHOT.war/WEB-INF/lib/test-webui-backend-1.0-SNAPSHOT.jar Weld BeanManager [bean count=76 ]

如果在 EAR 中直接包含的库中使用: 为 test-ear-1.0-SNAPSHOT.ear/test-ejb3-dao-1.0-SNAPSHOT.jar/ Weld BeanManager [bean count=224]

所以这些 BeanManagers 似乎“负责”应用程序的不同部分。这是真的吗?

最佳答案

这由了解 EAR 规范的应用程序服务器处理。

来自维基百科:

Most application servers load classes from a deployed EAR file as an isolated tree of Java classloaders, isolating the application from other applications, but sharing classes between deployed modules. For example, a deployed WAR file would be able to create instances of classes defined in a JAR file that was also included in the containing EAR file, but not necessarily those in JAR files in other EAR files. One key reason for this behavior is to allow complete separation between applications which use static singletons (e.g. Log4J), which would otherwise confuse the configuration between separate applications. This also enables different versions of applications and libraries to be deployed side-by-side.

因此每个模块都有自己的BeanManager,而应用服务器管理这些实例之间的依赖关系。

关于java - 了解多模块应用中的 CDI/Weld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22807435/

相关文章:

java - 从jsp页面的多选复选框转换参数

java - 如何在 CDI 注入(inject)的字段中从父类(super class)转换为派生类?

java - 在 Java CDI 拦截器中获取调用者类

jakarta-ee - 如果通过 Provider.get() 获取 @Dependent 作用域的 CDI bean,何时会销毁该 bean?

jakarta-ee - 使用服务生产者的 CDI PostConstruct

java - 未注入(inject)内部依赖项(Weld + JavaSE)

java - Spring Boot传递局部变量

java - 用java显示数据库中的Blob

java - 如何在 ubuntu 中启动、停止和重新启动 bash 脚本

eclipse - JSF Maven Tomcat 应用程序突然停止从 eclipse 工作