嗨,我在将常规 java 类(Pojo 类)注入(inject) 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类:
public class Pojo {
}
在我的多项目中,结构如下:
ROOT - 耳朵
Web 模块依赖于 ejb。
在 web 模块中是 beans.xml 与 bean-discovery-mode="all"
最佳答案
我通过向 ejb 添加另一个 beans.xml 解决了我的问题。
我把这个文件放到
main/java/resources/META-INF 本地化。
为什么?
One of the most significant changes in CD 1.1/Java EE 7 is that by popular request, CDI is now enabled by default. This means that there's no need to explicitly add a beans.xml to enable DI any more. 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. 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.
但我想为 ejb 模块和 war 模块创建一个 beans.xml。
关于java - CDI 不能注入(inject) pojo java 类。 (带有限定符@Default 的 Pojo 类型的依赖关系不满足),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33697807/