我使用 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 的对象被注入(inject)对象 A 并调用方法测试。为什么?据我所知,它不能被注入(inject)。
编辑 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>
但这并没有帮助。
最佳答案
您必须在 beans.xml 中指定 1.1 版本。
version="1.1" bean-discovery-mode="annotated"
现在您的 bean 部署存档是用于 Weld 的显式存档。
查看详情:enter link description here
关于java - CDI:bean-discovery-mode=annotated 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30432295/