从 Quarkus 扩展中公开 CDI bean

Exposing CDI beans from Quarkus extensions

我正在尝试实现一个基于运行时配置的 Quarkus 扩展,它提供了一个 SecurityIdentityAugmentor。

部署:

我的处理器

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep 
private MyMarkerConfiguredBuildItem setupAugmentor(MyRecorder recorder, MyAugmentorConfig config, BeanContainerBuildItem beanContainer) {
    recorder.sertConfig(config, beanContainer.getValue();
    return new MyMarkerConfiguredBuildItem ();
}

运行时间:

  1. 我的录音机:
public void setConfig(MyAugmentorConfig config, BeanContainer beanContainer) {
    beanContainer.instance(MyProducer.class).setConfig(config);
}
  1. 我的制作人:
@ApplicationScoped
public class MyProducer {

    public void setConfig(MyConfig config) {
        this.config = config;
    }

    @Produces
    @ApplicationScoped
    public MyAugmentor createAugmentor() {
        return new MyAugmentor(this.config);
    }
}

我可以在客户端应用程序中生成 MyAugmentor 实例的唯一方法是在扩展的运行时模块中添加 beans.xml。但是,我在 github 存储库的其他扩展中没有看到 beans.xml。有人能给我指出正确的方向吗?

您只需要使用构建步骤在您的扩展的部署模块中注册您的生产者 bean(因为它不会被自动发现,因为扩展没有索引):

@BuildStep
public AdditionalBeanBuildItem producer() {
    return new AdditionalBeanBuildItem(MyProducer.class);
}

生产者必须在您的扩展的运行时模块中,您可以在其中注入您的配置。

@ApplicationScoped
public class MyProducer {

    @Inject MyConfig config;

    @Produces
    @ApplicationScoped
    public MyAugmentor createAugmentor() {
        return new MyAugmentor(this.config);
    }
}

许多现有扩展中都使用了此构造,例如,您可以在此处查看我的 Firestore 扩展:https://github.com/quarkiverse/quarkus-google-cloud-services/tree/master/firestore