不會將同時使用 Specializes 及 Alternative 註釋的類別注入其他模組

此規則會標示同時以 @Specializes@Alternative 註釋標註的類別。如果其他模組中的某個類別參照此類別,則不會將此類別注入其他模組,除非此類別在其他模組的 META-INF/beans.xml 中列為 <alternative>

在下列範例中,AltClass3 類別在 jar2/META-INF/beans.xml 檔中列為 <alternative>。此類別在 jar1/META-INF/beans.xml 檔中沒有列為 <alternative>


WEB-INF/lib/jar1: Class1 {
    @Inject @SomeQualifer String foo;
  }
 
WEB-INF/lib/jar2: Class2 { @Produces @SomeQualifer String doFoo() { ... } }
  @Alternative @Specializes AltClass3 extends Class2 { @Produces @SomeQualifer String doFoo() { ... } } 

在 Contexts and Dependency Injection (CDI) 1.0 實作中,Class2 將會注入並用作 Class1.foo 的產生器。 在 CDI 1.2 實作中,Class2 不會注入,且應用程式不會啟動,同時還會說明找不到 Class1.foo 的產生器。

您可以使用下列兩種方法解決此問題:

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