[英]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 - 构建我的第一个扩展,但基本上这些是创建配置所采取的步骤:
maven
multi module project (not the project where the configuration will be consumed).maven
多模块项目(不是将消耗配置的项目)。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
.这将在那里创建具有以下模块的“子多模块”项目:
runtume
和deployment
。
runtime
module and add and annotate your config class.runtime
模块并添加和注释您的配置类。 It should be something similar to this classruntime
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;
}
}
@Recorder
public class OtaibeKeycloakRecorder {
public void initOtaQuarkusProducer(BeanContainer container, OtaibeKeycloakConfig configuration) {
OtaibeKeycloakQuarkusProducer producer = container.instance(OtaibeKeycloakQuarkusProducer.class);
producer.initialize(configuration);
}
}
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 .你可以在这里找到我的实现。
Now, go to your initial project where the configuration will be consumed and add the runtime
module as a dependency.现在,转到将使用配置的初始项目,并将
runtime
模块添加为依赖项。
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.