Spring Boot项目中application-default.yaml是否会被加载?
Spring Boot application-default 配置文件额外加载场景
default 是 Spring Boot 内置的特殊默认 Profile,除了主动通过启动参数 -Dspring.profiles.active=default 激活外,还有以下场景会自动加载 application-default.yaml/properties:
- 未显式激活任何 Profile 时:如果项目启动时没有通过任何渠道指定
spring.profiles.active的取值,Spring Boot 会默认激活defaultProfile,此时application-default配置会被自动加载,优先级高于全局主配置application.yaml。 - 激活 Profile 列表包含 default 时:不管是在
application.yaml中配置spring.profiles.active: develop,default,还是通过环境变量、配置中心等其他渠道指定的激活 Profile 列表包含default,都会加载对应配置,不需要单独通过启动参数指定。 - 通过
spring.profiles.include引入 default 时:无论是主配置还是其他已激活的环境配置中,通过spring.profiles.include配置指定包含defaultProfile,也会触发该配置文件加载,常用于多 Profile 功能拆分的组合场景。 - Profile 分组包含 default 时:Spring Boot 2.4+ 支持 Profile 分组能力,如果你配置了类似
spring.profiles.group.prod: prod,default,那么激活prod分组时,default会作为组内 Profile 被自动激活,对应配置也会加载。 - 单元测试未指定生效 Profile 时:如果单元测试使用
@ActiveProfiles注解时没有指定任何生效 Profile,或者指定的 Profile 列表包含default,也会加载该配置文件。
注意:
application-default配置的优先级低于你主动指定的环境专属配置,比如同时激活prod和default时,同 key 的配置会以application-prod.yaml中的取值为准。
内容的提问来源于stack exchange,提问作者Satya
相关产品推荐
相关产品推荐

