@NacosPropertySource、@NacosPropertySources、@NacosValue、@NacosConfigurationProperties

@NacosPropertySource
用来配置单个DataId
如:
@NacosPropertySource(dataId = "user.info", groupId = "springbootvue", autoRefreshed = true)

@NacosPropertySources
用来配置多个DataId
如:
@NacosPropertySources({
        @NacosPropertySource(dataId = "user.info",  groupId = "springbootvue", autoRefreshed = true),
        @NacosPropertySource(dataId = "user.info.pre",  groupId = "springbootvue", autoRefreshed = true),
        @NacosPropertySource(dataId = "user.info.test",  groupId = "springbootvue", autoRefreshed = true),
        @NacosPropertySource(dataId = "user.info.test",  groupId = "springbootvuetest", autoRefreshed = true)})

@NacosValue
用来获取Nacos中的值

但有个问题!
@NacosValue不能区分DataId。
也就是说,当用@NacosValue方式获取的key属性只能在所有接入的多DataId中是唯一的属性名,不然会造成取值错误。
所以Nacos提供了@NacosConfigurationProperties注解。
是不是很熟悉?对的,和@ConfigurationProperties是一样的道理。
定义并接收指定配置中的属性值
如:
@NacosConfigurationProperties(dataId = "user.info", groupId = "springbootvue", autoRefreshed = true)
然后在类中,定义和Nacos配置的key一样的属性名,就可以了!
这种方式支持StringInt等基本类型,像ListMapObject等特殊类型,是不支持的!需要用string接收,然后在对应set方法中重新处理一下
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值