Spring Boot-YML配置-合并时擦除条目 [英] Spring Boot - YML config - Erase entry when merging
问题描述
在类路径中,我的应用具有基本的YML配置,如下所示:
I have a base YML config for my app, in the class path that looks like this:
hello-world:
values:
bar:
name: bar-name
description: bar-description
foo:
name: foo-name
description: foo-description
hello-world包含从字符串到POJO的映射,称为值。我想覆盖hello-world中的设置,尤其是要删除一个条目。因此,在运行应用程序的本地目录上,我有此应用程序。yml:
hello-world contains a map from string to POJO called values. I want to override the settings in hello-world, in particular I want to delete an entry. So on the local directory where I run the app I have this application.yml:
hello-world:
values:
bar:
name: bar-name
description: bar-description
source: from-the-local-dir
但这不起作用,因为当我的本地配置覆盖现有配置时,地图将合并为一个,并且原始条目为 foo 保持。有没有一种方法可以在Spring yml中从配置映射中显式删除条目?
But this doesn't work, because when my local config overrides the existing one, the maps are merged into one and the original entry "foo" is kept. Is there a way to explicitly delete entries from a config map in spring yml?
PS:我可以看到,通过修改 在本地文件中的条目。这是完整的代码,我添加了一个源配置来告诉最后加载的文件:
PS: I can see that the local file get picked up, by modifying the the "bar" entry in the local file. Here's the full code, I've added a "source" config to tell which file was loaded last:
@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();
}
}
}
推荐答案
默认情况下,春季启动会从src / main / resource / application.yml中获取文件。
您可以声明config / application.yml,这些配置将覆盖src / main / resources中的application.yml。
Spring boot by default takes file from src/main/resource/application.yml You can declare config/application.yml and those config will override application.yml in src/main/resources
您可以尝试src / main / resources / application.yml:
You can just try src/main/resources/application.yml:
hello-world:
bar:
name: bar-name
description: bar-description
foo:
name: foo-name
description: foo-description
和config / application.yml
And config/application.yml
hello-world:
bar:
name: bar-name
description: bar-description
我认为这可以帮助
因此,当您运行应用程序config / application.yml时,它将覆盖现有的src / main / resources / application.yml
I think this can help So when you will run your application config/application.yml will override your existing src/main/resources/application.yml
您可以从config / application中完全删除hello-world .yml
但它将引发运行时异常,例如:
You can completely remove hello-world from config/application.yml But it will throw a runtime exception something like :
Could not resolve placeholder 'hello-world.foo' in value "${hello-world.foo}
要解决此问题,您可以应用注入值,例如:
@Value( $ {hello-world.foo:})
在':'之后可以定义默认值
To fix it you can apply injecting value like: @Value("${hello-world.foo:}") where after ':' you can define default value
您可以在config / application.yml中留空字段
You can leave in config/application.yml empty fields
hello-world:
bar:
name: bar-name
description: bar-description
foo:
name:
description:
默认情况下,如果您不指定foo中的所有值,将为empty(''),然后可以从地图中过滤并删除具有空值的条目。
您也可以研究此类:
By default if you will not specify all values in foo will be empty('') and then you can filter and remove entry with empty values from your map. Also you can look into this class:
这篇关于Spring Boot-YML配置-合并时擦除条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!