
spring boot 支持通过逗号分隔的 profiles 激活多个配置文件,实现 application.properties(默认)与多个 profile-specific 文件(如 application-dev.properties、application-dev-eu1.properties)的自动叠加加载,无需额外编码即可完成层级化配置管理。
spring boot 支持通过逗号分隔的 profiles 激活多个配置文件,实现 application.properties(默认)与多个 profile-specific 文件(如 application-dev.properties、application-dev-eu1.properties)的自动叠加加载,无需额外编码即可完成层级化配置管理。
在 Spring Boot 中,配置文件的加载遵循明确的优先级和叠加规则:application.properties 作为基础配置始终被加载;所有激活的 profile 对应的 application-{profile}.properties 文件会按激活顺序依次合并(后加载的覆盖先加载的同名属性)。因此,要同时加载 application.properties、application-dev.properties 和 application-dev-us1.properties,只需将多个 profile 以逗号分隔方式激活:
# 启动时指定多个 active profile java -Dspring.profiles.active=dev,dev-us1 -jar myapp.jar
或通过 application.properties 配置:
# application.properties spring.profiles.active=dev,dev-us1
此时 Spring Boot 将按以下顺序加载并合并配置:
- application.properties(默认基础配置)
- application-dev.properties(通用开发配置)
- application-dev-us1.properties(US1 区域特有配置,可覆盖前两者中同名属性)
✅ 关键说明:
- spring.profiles.active 支持多 profile,用英文逗号分隔,无空格;
- 所有匹配 application-{profile}.properties 的文件均会被加载(包括嵌套 profile 如 dev-us1);
- 配置覆盖遵循“后加载优先”原则,适合构建 dev → region 的配置继承链;
- 注意 profile 名称需严格匹配文件后缀(如 dev-us1 对应 application-dev-us1.properties,不可写作 dev_us1 或 dev-us-1);
- 若使用 @Profile 注解限定 Bean,需确保其 profile 名与激活列表一致(支持逻辑或,如 @Profile({"dev", "dev-us1"}))。
? 提示:可通过 /actuator/env 端点验证实际生效的 profiles 及配置来源,便于调试配置叠加结果。











