我在网上找不到明确的答案。
Spring Boot的yml文件是否会“继承”彼此?我的意思是如果我有:
application.yml
同时包含以下内容:
server:
port: 80
host: foo
和 application-profile1.yml
,它只有
server:
port: 90
如果我使用profile1
作为激活的配置文件来启动Spring Boot,那么server.host
属性也会被设置为foo
吗?
我在网上找不到明确的答案。
Spring Boot的yml文件是否会“继承”彼此?我的意思是如果我有:
application.yml
同时包含以下内容:
server:
port: 80
host: foo
和 application-profile1.yml
,它只有
server:
port: 90
如果我使用profile1
作为激活的配置文件来启动Spring Boot,那么server.host
属性也会被设置为foo
吗?
是的,application.yml
文件优先级高于任何 application-{profile}.yml
文件。来自特定于配置文件的属性将覆盖默认 application.yml
文件中的值,并且不存在于 特定于配置文件的属性将从默认文件中加载。这也适用于 .properties
文件以及 bootstrap.yml
或 bootstrap.properties
。
Spring Boot 文档在72.7 更改配置取决于环境段落中提到了它:
在此示例中,默认端口为 9000,但如果 Spring profile 'development' 处于活动状态,则端口为 9001,如果 'production' 处于活动状态,则为 0。
YAML 文档按遇到时合并顺序(因此后面的值会覆盖之前的值)。
使用
application-$ {profile} .properties
来指定特定于配置文件的值可以完成相同的事情。
application.yml
的文件:spring:
profiles: default-server-config
server:
port: 9801
servlet:
context-path: '/ctp'
如果我想要使用default-server-config
配置文件,并在我的application-dev.yml
中使用8080
端口。
application-dev.yml
文件内容如下:
spring:
profiles:
include:
- default-server-config
- dev-config
---
spring:
profiles: dev-config
server:
port: 8080
-Dspring.profiles.active=dev
。
application-{profile}.properties
会从基础配置文件application.properties
继承,而在这种情况下无法进行多级继承。主要是因为在这种情况下,继承的顺序并不直观。 - Szymon Stepniak