Quarkus / CDI 和 "java config" DI 定义

Quarkus / CDI and "java config" DI definitions

我刚刚开始 quarkus 概念验证。容器启动时间太棒了!

现在,我正在研究依赖注入部分。并找出选项。

https://quarkus.io/blog/quarkus-dependency-injection/

我的偏好是:

我更喜欢构造函数注入。 (一切顺利)。

我更喜欢 "java config",因此我可以遵循 "Composition Root" 模式,将我所有的应用程序依赖项注入放在一个公共位置。 (见 https://blog.ploeh.dk/2011/07/28/CompositionRoot/

使用 Spring DI,这是通过

完成的

org.springframework.context.annotation.Configuration

并在那里声明 Bean。

Aka,我不想在我的 类 中到处放置“@ApplicationScoped”注释。

CDI/Quarkus 是否支持 "java config" 模型?我询问 quarkus 的原因是我读到 quarkus 的 CDI 实现有限。

//start quote//Our primary goal was to implement a supersonic build-time oriented DI solution compatible with CDI. This would allow users to continue using CDI in their applications but also leverage Quarkus build-time optimizations. However, ArC is not a full CDI implementation verified by the TCK - see also the list of supported features and the list of limitations.//end quote

所以我的问题不仅仅是 CDI 问题。

我尝试了不同的互联网搜索词,但它们一直向我显示 Spring 链接。 :(

您应该创建一个将生成您的 bean 的 CDI bean,这是 Spring 调用 Java 配置的标准 CDI 方法。

所以像这样

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;


@ApplicationScoped
public class MyConfiguration {
    @Produces
    public MyBean myBean(){
        return new MyBean();
    }
}