我正在研究dropwizard项目,我想根据是否启用配置值来设置其他配置源提供程序。
由于无法在initialize
中访问配置对象,因此创建了全局引导变量。虽然我仍然无法更新配置值。
有关如何解决此问题的任何帮助或指导?
private Bootstrap<MyConfiguration> bootstrap;
@Override
public void initialize(final Bootstrap<MyConfiguration> bootstrap) {
super.initialize(bootstrap);
this.bootstrap = bootstrap;
}
@Override
public void run(final MyConfiguration configuration,
final Environment environment) {
this.bootstrap.setConfigurationFactoryFactory(
new ImportConfigurationFactoryFactory<>());
if(configuration.isEnabled()) {
this.bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
this.bootstrap.getConfigurationSourceProvider(),
new StrSubstitutor(new ConfigSwitcher())));
} else {
this.bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
this.bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)));
}
}