【发布时间】:2019-04-10 20:55:31
【问题描述】:
我的应用有一个基本的 YML 配置,在类路径中如下所示:
hello-world:
values:
bar:
name: bar-name
description: bar-description
foo:
name: foo-name
description: foo-description
hello-world 包含一个从字符串到 POJO 的映射,称为值。我想覆盖 hello-world 中的设置,特别是我想删除一个条目。所以在我运行应用程序的本地目录上,我有这个 application.yml:
hello-world:
values:
bar:
name: bar-name
description: bar-description
source: from-the-local-dir
但这不起作用,因为当我的本地配置覆盖现有配置时,地图会合并为一个,并保留原始条目“foo”。有没有办法在 spring yml 中从配置映射中显式删除条目?
PS:通过修改本地文件中的“bar”条目,我可以看到本地文件被拾取。这是完整的代码,我添加了一个“源”配置来告诉最后加载哪个文件:
@Import(PlayGround.Config.class)
@SpringBootApplication
public class PlayGround {
@Autowired
Config config;
@Value("${source}")
String source;
public void start() {
System.out.println(config);
System.out.println(source);
}
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
ConfigurableApplicationContext context = SpringApplication.run(PlayGround.class, args);
PlayGround playGround = context.getBean(PlayGround.class);
playGround.start();
}
@ConfigurationProperties(prefix = "hello-world")
public static final class Config {
Map<String, Information> values = new HashMap<String, Information>();
public Map<String, Information> getValues() {
return values;
}
public void setValues(Map<String, Information> values) {
this.values = values;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("values", values)
.toString();
}
}
public static final class Information {
String name;
String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.add("description", description)
.toString();
}
}
}
【问题讨论】:
标签: java spring spring-boot configuration yaml