Spring 配置别写死:先分 profile,再谈环境变量覆盖
把数据库密码写进 application.yml 再提交,是最常见的回滚成本。更稳的顺序是:
本地用
application-local.yml(不进库)或环境变量;用
spring.profiles.active区分 local / dev / prod;生产密钥走环境变量、密钥管理服务或挂载文件,而不是镜像层。
排查时先看生效顺序:命令行参数、环境变量、profile 专属文件、默认文件。同一 key 被多处覆盖时,打印一次 Environment 有效值,比猜「到底读没读到」快。新项目宁可先把密钥外置做对,再谈优雅的多模块拆分。
来源
[1] Externalized Configuration,Spring Boot Docs,查阅于2026年10月24日。
#Spring #SpringBoot #配置 #profile #教程


成为第一条评论!
Nobody's responded to this post yet. Add your thoughts and get the conversation going.