【问题标题】:CDI: bean-discovery-mode=annotated is ignoredCDI: bean-discovery-mode=annotated 被忽略
【发布时间】:2015-05-25 06:19:47
【问题描述】:

我使用weld 作为CDI 容器。此外,我使用 osgi (felix)。所以它是 javase + felix+weld+pax。我有以下 beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"
       bean-discovery-mode="annotated">
</beans>

我有两个班级:

@ApplicationScoped
public class A {
  @Inject
  private B b;
  public void postCreate(@Observes ContainerInitialized event, BundleContext ctx) {
   b.test();
  }
}

B类

public class B{
  public void test(){
  System.out.println("test is here");
  }
}

如您所见,B 类没有任何@scopes 或@dependent 注释。但是,当我启动应用程序时,B 类的对象被注入到对象 A 并调用方法测试。为什么?据我了解,它不能被注入。

编辑 1
我尝试使用 1.1 版本:

<?xml version="1.0" encoding="UTF-8"?>
<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="annotated" version="1.1">
</beans>

但它没有帮助。

【问题讨论】:

  • 您使用的是实现 CDI 1.0 的 1.x 版本的焊接吗?
  • @Sebastian S 我正在使用实现 CDI 1.1 的 2.2.10 版本。

标签: java osgi cdi apache-felix weld


【解决方案1】:

您必须在 beans.xml 中指定 1.1 版本。

version="1.1" bean-discovery-mode="annotated"

现在您的 bean 部署存档是 Weld 的显式存档。
查看详情:enter link description here

【讨论】:

  • 是的。 CDI 使用它,因为当我设置 bean-discovery-mode="annotated2" 时它会抛出异常。
【解决方案2】:

我在焊接邮件列表中写了这篇文章,这是 Jozef Hartinger(主要焊接开发人员之一)所写的

到目前为止,Pax CDI 似乎只实现了显式 bean 存档。我在https://ops4j1.jira.com/browse/PAXCDI-186 提交了一个问题 你需要标记你不想被发现的类/包 @Vetoed 或使用排除过滤器 http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#exclude_filters

【讨论】: