[英]CDI can not inject pojo java class. (Unsatisfied dependencies for type Pojo with qualifiers @Default)
Hi i have a problem with inject a regular java class (Pojo class) into EJB bean.嗨,我在将常规 Java 类(Pojo 类)注入 EJB bean 时遇到问题。
@ApplicationScoped
public class DomainRouteFinderService {
@Inject
private Pojo pojo;
private AtomicInteger sequencer;
@PostConstruct
private void init(){
sequencer = new AtomicInteger();
sequencer.lazySet(0);
}
@Produces
@Named("sequencer")
public String getText(){
return "Number: "+ sequencer.getAndIncrement();
}
}
Pojo class: Pojo类:
public class Pojo {
}
In my multiprojects with structure like below:在我的多项目中,结构如下:
ROOT - ear根 - 耳朵
Web module depends on ejb. Web 模块依赖于 ejb。
In web module is beans.xml with bean-discovery-mode="all"在 web 模块中是 beans.xml 和 bean-discovery-mode="all"
I resolved my problem by adding another beans.xml to ejb.我通过向 ejb 添加另一个 beans.xml 解决了我的问题。 I put this file to main/java/resources/META-INF localization.
我把这个文件放到 main/java/resources/META-INF 本地化。
Why?为什么?
One of the most significant changes in CD 1.1/Java EE 7 is that by popular request, CDI is now enabled by default.
CD 1.1/Java EE 7 中最重要的变化之一是,根据流行的要求,现在默认启用 CDI。 This means that there's no need to explicitly add a beans.xml to enable DI any more.
这意味着不再需要显式添加 beans.xml 来启用 DI。 However, it is also very important to understand that CDI now also provides finer grained control over component scanning via the 'bean-discovery-mode' attribute.
但是,了解 CDI 现在还通过“bean-discovery-mode”属性对组件扫描提供更细粒度的控制也非常重要。 This attribute has three possible values:
此属性具有三个可能的值:
'annotated' - loosely translated, means that only components with a class-level annotation are processed. 'all' - all components are processed, just like they were in Java EE 6 with the explicit beans.xml. 'none' - CDI is effectively disabled.
By default, if you specify nothing and there is no beans.xml, the bean discovery mode of 'annotated' and not 'all' is assumed.
默认情况下,如果您不指定任何内容并且没有 beans.xml,则假定为 'annotated' 而不是 'all' 的 bean 发现模式。
But i want to one beans.xml for ejb module and war module.但我想为 ejb 模块和 war 模块创建一个 beans.xml。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.