當沒有 beans.xml 檔時,CDI 會掃描是否有隱含的 Bean

Contexts and Dependency Injection (CDI) 1.1 規格引進隱含的 Bean 探索模式,導致 Java Platform Enterprise Edition (Java EE) 6 CDI 1.0 實作在 WebSphere Application Server 中的行為和效能變更。

如果專案不包含 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 中設定這些內容的相關資訊,請參閱用於在 WebSphere Application Server 中改進應用程式啟動的自訂內容文件。

如需 Java EE 7 CDI 1.2 實作的相關資訊,請參閱 Contexts and Dependency Injection 1.2 行為變更