[英]How to use configuration maps for custom Quarkus ConfigProperties
For my Quarkus application I'm looking for a way to define a configuration map from within a custom ConfigProperties class.对于我的 Quarkus 应用程序,我正在寻找一种从自定义 ConfigProperties class 中定义配置 map 的方法。 I tried the following:我尝试了以下方法:
import io.quarkus.arc.config.ConfigProperties;
import io.quarkus.runtime.annotations.ConfigItem;
@ConfigProperties(prefix = "my-properties")
public class MyPropertiesConfiguration {
@ConfigItem
public Map<String, FooConfiguration> foo;
// ...
}
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
@ConfigGroup
public class FooConfiguration {
@ConfigItem
public String myProperty;
}
Given those two classes and the following application.properties file...鉴于这两个类和以下 application.properties 文件...
my-properties.foo.anystring.my-property=bar
on startup the application fails with error message: javax.enterprise.inject.spi.DeploymentException: No config value of type [java.util.Map] exists for: my-properties.foo
启动时应用程序失败并显示错误消息: javax.enterprise.inject.spi.DeploymentException: No config value of type [java.util.Map] exists for: my-properties.foo
As far as I understandhttps://quarkus.io/guides/writing-extensions#configuration-maps the sample should work.据我了解https://quarkus.io/guides/writing-extensions#configuration-maps该示例应该可以工作。 What am I doing wrong?我究竟做错了什么? Could it happen that this functionality is just limited to Quarkus extensions only?此功能是否仅限于 Quarkus 扩展?
As written in this Quarkus github issue , this is currently not supported.正如此Quarkus github 问题中所写,目前不支持此功能。
My dirty workaround was to use the ConfigProvider
directly.我肮脏的解决方法是直接使用ConfigProvider
。 Use with care.小心使用。
public static Map<String, String> getMapFromConfig(String prefix) {
final Config config = ConfigProvider.getConfig();
final Iterable<String> propertyNames = config.getPropertyNames();
return StreamSupport.stream(propertyNames.spliterator(), false)
.filter(name -> name.startsWith(prefix) && !name.equalsIgnoreCase(prefix))
.collect(
Collectors.toMap(
propertyName -> cleanupPropertyName(propertyName.substring(prefix.length() + 1)),
propertyName -> config.getOptionalValue(propertyName, String.class).orElse("")));
}
/** Remove start and end double quotes */
public static String cleanupPropertyName(String name) {
if (name.startsWith("\"") && name.endsWith("\"")) {
return name.substring(1, name.length() - 1);
}
return name;
}
My config looks like this:我的配置如下所示:
property-templates:
"my.key": value 1
"my.second.key": value 2
Declare the configuration like this像这样声明配置
import io.quarkus.arc.config.ConfigProperties;
@ConfigProperties(prefix = "myapp")
public class AppSpecificConfig {
public String property;
}
The application.properties
file will contain application.properties
文件将包含
myapp.property=foo
And then you can @Inject
an instance of this class anywhere within your application.然后您可以在应用程序中的任何位置@Inject
此 class 的实例。
For more details, see https://quarkus.io/guides/config#using-configproperties有关更多详细信息,请参阅https://quarkus.io/guides/config#using-configproperties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.