
Spring Boot 3.0 起废弃 spring.profiles,必须改用 spring.config.activate.on-profile;同时需注意 spring.profiles.active 与 on-profile 不可共存于同一配置块,否则启动报错。
spring boot 3.0 起废弃 `spring.profiles`,必须改用 `spring.config.activate.on-profile`;同时需注意 `spring.profiles.active` 与 `on-profile` 不可共存于同一配置块,否则启动报错。
Spring Boot 3.0 对配置文件的解析机制进行了重大重构,核心变化之一是彻底移除了 YAML/Properties 中直接使用 spring.profiles(如 profiles: dev)的语法支持。该属性自 2.4 版本起已被标记为弃用,至 3.0 正式删除——即使启用了 spring.config.use-legacy-processing: true,该兼容开关仅适用于旧版配置加载逻辑的底层行为(如 profile 激活顺序、文件定位等),并不恢复对已删除属性的语法解析。因此,InvalidConfigDataPropertyException 报错无法通过开启 legacy 模式规避,必须进行显式迁移。
✅ 正确迁移方式
需将所有 spring.profiles: xxx 形式的配置块头部,替换为标准的 spring.config.activate.on-profile: xxx 结构,并确保其嵌套层级准确:
# application.yml
spring:
profiles.active: dev # ✅ 全局激活 profile 仍合法(仅此处允许)
rails:
url: https://dev-rails.example.com
userIdentifier: DEV_USER
service: FILESERVICE
methodInBody: POST
uri: /api/external-interfaces/file-locations
---
spring:
config:
activate:
on-profile: stage # ✅ 替换原 'profiles: stage'
rails:
url: https://stage-rails.example.com
userIdentifier: STAGE_USER
service: FILESERVICE
methodInBody: POST
uri: /api/external-interfaces/file-locations
jackson:
serialization:
indent-output: true
---
spring:
config:
activate:
on-profile: prod # ✅ 替换原 'profiles: prod'
rails:
url: https://prod-rails.example.com
userIdentifier: PROD_USER
service: FILESERVICE
methodInBody: POST
uri: /api/external-interfaces/file-locations⚠️ 关键注意事项
- 禁止混用:spring.profiles.active(全局激活)可单独存在,但任何包含 spring.config.activate.on-profile 的配置片段中,绝对不可再出现 spring.profiles: xxx 或 profiles: xxx,否则 Spring Boot 会拒绝加载并抛出 InvalidConfigDataPropertyException。
- 层级严格:on-profile 必须位于 spring.config.activate 下,缩进必须正确(YAML 对空格敏感),错误写法如 spring.config.activate.on-profile: dev(顶格)或缩进不一致均会导致解析失败。
-
多 profile 激活:若需匹配多个 profile(如 dev 或 test),可使用 SpEL 表达式:
spring: config: activate: on-profile: "dev | test" - 条件组合:支持与 on-environment 等联合使用,实现更精细的配置激活策略。
? 验证建议
升级后务必执行以下检查:
- 运行 mvn spring-boot:run 或启动应用,确认无 InvalidConfigDataPropertyException;
- 通过 /actuator/env 端点验证当前激活的 profile 及对应配置是否正确注入;
- 对涉及 profile 切换的功能(如数据库连接、外部服务地址)进行集成测试。
完成上述迁移后,配置即完全符合 Spring Boot 3.x 规范,兼顾可维护性与未来版本兼容性。










