多个@PropertySource导致配置文件读取错误

背景:
有2个配置文件内容格式一样.
文件1:

login.acctID=xxxxxxx
login.username=aaaaaa
login.password=bbbbbb
login.lcid=eee

文件2:

login.acctID=xxxxxxx
login.username=cccccc
login.password=dddddd
login.lcid=fff

然后在 SpringBoot 中2个类用 @PropertySource 来获取属性值. 导致2个类获取到的值都是一样的.
然后度娘搜了半天没搜到, Google搜了下, 第一个就是…
原因是多个 properties 中, 同样的 key 值,最后一个处理的 @PropertySource 会覆盖前面的.

You can’t. The two property sources will be merged into the same Spring Environment. The last declared value for the same key in the your .properties files will override any previous value for the same key. If your read the JavaDoc of @PropertySource you will find the following statement:

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will ‘win’ and override.


参考:
https://stackoverflow.com/questions/35246297/how-to-refer-to-separate-propertysource-in-propertysources-to-get-value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值