首页 > 解决方案 > 从 Quarkus 扩展中公开 CDI bean

问题描述

我正在尝试实现一个基于运行时配置的 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。有人能指出我正确的方向吗?

标签: quarkus

解决方案


您只需要使用构建步骤在扩展的部署模块中注册您的生产者 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


推荐阅读