java - CDI 不能注入(inject) pojo java 类。 (带有限定符@Default 的 Pojo 类型的依赖关系不满足)

标签 java jakarta-ee gradle cdi

嗨,我在将常规 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 - 耳朵
  • ejb
  • 网站

  • 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/

    相关文章:

    java - 使用 ArrayList 进行选择排序

    android - 排除或覆盖 AAR list

    android - 找不到 com.android.tools.build :gradle:4. 0.1

    groovy - 仅当Groovy中新值不为空或为null时,分配映射条目的简便方法

    java - 静态方法返回空值

    java - 对 Java 集合进行排序

    java - 如何在spock测试中模拟私有(private)方法的返回值

    spring - 找不到类[org.springframework.orm.hibernate5.LocalSessionFactoryBean]

    java - Glassfish 使用 URL 重写,即使我没有关闭 cookie

    java - 与 SINGLE_TABLE 继承的子类的关系