【发布时间】:2019-02-12 17:07:48
【问题描述】:
我目前正在寻找一种动态组装配置键(回退处理)的方法,然后在我们的 microprofile-config.properties 文件中查找它们。这样的文件可能如下所示:
# customer fallbacks
my.config=1234 # use this fallback when there is no customer
customer2.my.config=12345 # use this fallback when there is no subcustomer
customer2.subCustomer1.my.config=123456 # first level
所以当有一个客户和一个子客户时,就用on
我遇到这个问题的原因是我想使用@ConfigProperty 注释,所以没有 ConfigProvider.getConfig()。这意味着我必须在我的自定义 ConfigSource 中组装我的动态配置密钥。
我知道 ConfigSources 是通过 ServiceLoader 在服务器启动时加载的。所以我尝试删除现有的配置并将其替换为我的自定义配置:
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
@Startup
@Singleton
public class StartupConfigurationRegistrar{
@PostConstruct
private void registerConfig() {
final Config customConfig = ConfigProviderResolver.instance().getBuilder().withSources(new FallbackHandlingConfiguration(myReqiredVariables)).addDefaultSources().build();
ConfigProviderResolver.instance().releaseConfig(ConfigProviderResolver.instance().getConfig());
ConfigProviderResolver.instance().registerConfig(customConfig, Thread.currentThread().getContextClassLoader());
}
}
我的 ConfigSource 已正确添加。但后来当尝试访问不同类中的配置时,我的自定义 ConfigSource 消失了,只剩下三个默认的 ConfigSource。我认为这可能是 ClassLoader 的问题。
知道如何在ConfigSource 中获取动态值吗?
【问题讨论】:
标签: jakarta-ee config microprofile