Yml配置文件“继承"使用Spring Boot [英] Yml config files "Inheritance" with Spring boot
问题描述
我在网上找不到直接的答案.
I couldn't find a straight answer online.
Spring Boot的yml文件是否彼此继承"?我的意思是,如果我有:
application.yml
具有
Do Spring Boot's yml files "inherit" from each other? I mean if I have:
application.yml
which has
server:
port: 80
host: foo
和application-profile1.yml
仅具有
server:
port: 90
因此,如果我以profile1
作为活动配置文件启动Spring Boot,我还将server.host
属性设置为foo
吗?
So if I start my Spring Boot with profile1
as active profile, will I also have server.host
property set to foo
?
推荐答案
是的,application.yml
文件的优先级高于任何application-{profile}.yml
文件.特定于配置文件的yml文件中的属性将覆盖默认application.yml
文件中的值,而特定于配置文件的yml文件中不存在的属性将从默认文件中加载.它适用于.properties
文件以及bootstrap.yml
或bootstrap.properties
.
Yes, application.yml
file has higher precedence over any application-{profile}.yml
file. Properties from profile specific yml file will override values from the default application.yml
file and properties that do not exist in profile specific yml file will be loaded from the default one. It applies to .properties
files as well as to bootstrap.yml
or bootstrap.properties
.
Spring Boot文档在 72.7根据环境更改配置 段落:
Spring Boot documentation mentions it in 72.7 Change configuration depending on the environment paragraph:
在此示例中,默认端口为9000,但如果Spring概要文件开发"处于活动状态,则该端口为9001,如果生产"为活动,则其为0.
In this example the default port is 9000, but if the Spring profile ‘development’ is active then the port is 9001, and if ‘production’ is active then it is 0.
YAML文档按照它们遇到的顺序进行合并(因此,较新的值将覆盖较早的值).
The YAML documents are merged in the order they are encountered (so later values override earlier ones).
要对属性文件执行相同的操作,可以使用application-${profile}.properties
指定特定于配置文件的值.
To do the same thing with properties files you can use application-${profile}.properties
to specify profile-specific values.
这篇关于Yml配置文件“继承"使用Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!