CDI 在没有 beans.xml 文件时扫描隐式 bean

Contexts and Dependency Injection (CDI) 1.1 规范引入了隐式 bean 发现方式,从而导致 WebSphere Application Server 中 Java Platform, Enterprise Edition (Java EE) 6 CDI 1.0 实现中的行为和性能更改。

如果项目不包含 beans.xml 文件,那么该规则标记 WEB-INFMETA-INF 文件夹。 从 Java EE 6 CDI 1.0 移至 Java EE 7 CDI 1.1 或更高版本时,隐式 bean 发现将导致所有 WAR 和 JAR 文件由运行时扫描,即使这些文件没有 beans.xml 文件也是如此。 如果启用此选项,那么可能会导致 Bean 发现性能下降。 cdi-1.2 Liberty上的功能部件。

在 Java EE 7 中,如果未定义 beans.xml 文件,运行时将自动扫描 bean 定义注释。要禁用该行为,请添加 beans.xml 文件并将 bean 发现方式设置为 none,如以下示例中所示。


<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
		  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		  		      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" 
		  		      bean-discovery-mode="none" version="1.1">
</beans>

在 Liberty 上,还可通过将以下属性添加到 server.xml 文件来禁用该行为。


<cdi12 enableImplicitBeanArchives="false"/>

在 WebSphere Application Server Traditional 上,可以通过以下方式禁用此行为:

有关在 WebSphere Traditional 中设置这些属性的更多信息,请参阅 Custom Properties for improving application startup in WebSphere Application Server 文档。

有关 Java EE 7 CDI 1.2 实现的更多信息,请参阅 Contexts and Dependency Injection 1.2 behavior changes