简体   繁体   English

quarkus / microprofile 中的组配置

[英]Group configuration in quarkus / microprofile

I want to group configuration items in Quarkus and use them as a Map .我想在 Quarkus 中对配置项进行分组并将它们用作Map Something along the lines of the next application.properties :类似于下一个application.properties的内容:

greeting.names = tom,jane
greeting.tom.message = hello
greeting.tom.name = tom
greeting.jane.message = hi
greeting.jane.name = jane

And have them in the application in a Map , like this:并将它们放在Map的应用程序中,如下所示:

@ConfigProperties
public class GreetingConfig {

   private String name;
   private String message;

   // getters / setters
}

And use them like this:并像这样使用它们:

private Map<String, GreetingConfig> config;

They are now name indexed, but a List would also be fine (and is what I actually need):它们现在已被名称索引,但List也可以(这也是我真正需要的):

greeting.1.message = hello
greeting.1.name = tom
greeting.2.message = hi
greeting.2.name = jane

Any ideas on how to realize this?关于如何实现这一点的任何想法? I have a programmatic solution, but would prefer a solution by annotation only我有一个编程解决方案,但更喜欢仅通过注释的解决方案

I've had a similar problem these days.这几天我也遇到了类似的问题。 So I wrote a simple Quarkus extension which helped me with the configuration.所以我写了一个简单的 Quarkus 扩展来帮助我进行配置。 You can use the guides from the Quarkus site: Quarkus - Writing Your Own Extension and Quarkus - Building my first extension , but basically these are the steps taken in order to create the configuration:您可以使用 Quarkus 站点中的指南: Quarkus - 编写您自己的扩展Quarkus - 构建我的第一个扩展,但基本上这些是创建配置所采取的步骤:

  1. Have some maven multi module project (not the project where the configuration will be consumed).有一些maven多模块项目(不是将消耗配置的项目)。
  2. Execute a similar command form the project directory:从项目目录执行类似的命令:
mvn io.quarkus:quarkus-maven-plugin:1.4.2.Final:create-extension -N \
    -Dquarkus.artifactIdBase=keycloak-extension \
    -Dquarkus.artifactIdPrefix=otaibe-commons-quarkus- \
    -Dquarkus.nameBase="Keycloak extension"

This will create 'sub multi module' project there with the following modules: runtume and deployment .这将在那里创建具有以下模块的“子多模块”项目: runtumedeployment

  1. Go to the runtime module and add and annotate your config class.转到runtime模块并添加和注释您的配置类。 It should be something similar to this class它应该类似于这个类
  2. In runtime module create a producer class which should register the configuration class as CDI bean:runtime模块中创建一个producer类,它应该将配置类注册为 CDI bean:
@ApplicationScoped
public class OtaibeKeycloakQuarkusProducer {

    private volatile OtaibeKeycloakConfig otaibeKeycloakConfig;

    void initialize(OtaibeKeycloakConfig config) {
        this.otaibeKeycloakConfig = config;
    }

    @Singleton
    @Produces
    public OtaibeKeycloakConfig otaibeKeycloakConfig() {
        return otaibeKeycloakConfig;
    }

}
  1. Create a Recorder class which should initialize the Producer :创建一个Recorder类,它应该初始化Producer
@Recorder
public class OtaibeKeycloakRecorder {

    public void initOtaQuarkusProducer(BeanContainer container, OtaibeKeycloakConfig configuration) {
        OtaibeKeycloakQuarkusProducer producer = container.instance(OtaibeKeycloakQuarkusProducer.class);
        producer.initialize(configuration);
    }

}
  1. In deployment module you have a Processor class.deployment模块中,您有一个Processor类。 Go there and register your Producer to be injectable as CDI bean and your Recorder to initialize it.去那里注册您的Producer以作为 CDI bean 和您的Recorder进行注入以对其进行初始化。 Add the similar code:添加类似的代码:
    @BuildStep
    AdditionalBeanBuildItem beans() {
        return AdditionalBeanBuildItem.builder().addBeanClasses(OtaibeKeycloakQuarkusProducer.class).build();
    }

    @BuildStep
    @Record(ExecutionTime.RUNTIME_INIT)
    void otaibeKeycloackConfigBuildItem(BeanContainerBuildItem beanContainer,
                                        OtaibeKeycloakConfig otaibeKeycloakConfig,
                                        OtaibeKeycloakRecorder recorder) {
        recorder.initOtaQuarkusProducer(beanContainer.getValue(), otaibeKeycloakConfig);
    }

You can find my implementation here .你可以在这里找到我的实现。

  1. Now, go to your initial project where the configuration will be consumed and add the runtime module as a dependency.现在,转到将使用配置的初始项目,并将runtime模块添加为依赖项。

  2. In order to ensure that the configuration is added properly execute the following maven command:为了确保正确添加配置,请执行以下maven命令:

mvn quarkus:generate-config

Now, you can check the file src/main/resources/application.properties.example and verify whether your properties are added there.现在,您可以检查文件src/main/resources/application.properties.example并验证您的属性是否已添加到那里。 The property group should start with quarkus.属性组应该以quarkus. plus the name of your @ConfigRoot annotation.加上您的@ConfigRoot注释的名称。 In my case for example it will start with quarkus.otaibe.keycloak例如,在我的情况下,它将以quarkus.otaibe.keycloak

That's it!就是这样!

Now in Quarkus 2.9, you do it with @ConfigMapping ( @ConfigProperties is deprecated).现在在 Quarkus 2.9 中,您可以使用@ConfigMapping@ConfigProperties已弃用)。

@StaticInitSafe
@ConfigMapping
public interface GreetingConfigMapping {
    Map<Integer, GreetingDetail> greeting();
}

interface GreetingDetail {
    String name();
    String message();
}

Try it in a @QuarkusTest ;@QuarkusTest中尝试; @Inject it. @Inject它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM