include 机制
Spring 提供了一个 spring.profiles.include 机制,用于将不同的 profile 包含进来,类似文本替换。这套机制在你使用 .properties 时没有什么问题,但是当你使用 yaml 来作为配置格式时,就会有问题。
比如你有这么 3 个配置文件:
# application-test1.yml
root:
a: 1
# application-test2.yml
root:
b: 2
# application.yml
root:
whatever: 3你使用 spring.profiles.include=test1,test2,想让 root 下面有 3 个属性(a, b, whatever),但是事实上按加载顺序做覆盖,而没有把几颗树合并。于是最后 root 下面只有一个属性(具体哪一个我也没做实验)。
如果你想避开这个问题,可以把 test1, test2 的配置文件使用 .properties 格式。生命不应该浪费给调试 Spring Profile 的加载逻辑。