- 引以为戒:指点江山(装逼)后请帮人解答疑惑(大佬的基本素养)
- 科普借鉴:https://cloud.tencent.com/developer/article/1522660
- @Value不支持复杂类型封装,但是我们可能有实际需要指定不同的全路径进行List、Map的接收(详见下文)。
- 属性默认值大家可以在评论区贴出来(赶需求中,临时记录一下)
- 配置信息(Nacos配置格式缩进用的Tab,复制需要修改缩进)
复制代码
1
2
3
4
5
6
7
8
9
10
11url: apk: download: defaultMap: com.swl.aplayb222: www.baidu.com0 com.swl.aplayb111: aplayb11 defaultList: - com.swl.aplayb111 - com.swl.aplayb222 testMap: '{"name": "zhangsan", "sex": "male"}' testList: com.swl.aplayb222,com.swl.aplayb111
- 方式1:接收testMap和testList
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26package com.xxl.config; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @RefreshScope @Component @Data /** * 参考: * https://www.cnblogs.com/javastack/p/13862164.html * */ public class URLNacosConfiguration1 { @Value("#{${url.apk.testMap}}") private Map<String, String> apkdownloadDefaultMap1; @Value("#{'${url.apk.testList}'.split(',')}") private List<String> apkTestList; }
- 方式2:接收defaultMap、defaultList
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.xxl.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @RefreshScope @Component @Data //读取远程配置文件 @ConfigurationProperties(prefix = "url.apk.download") //读取本地配置文件 //@PropertySource(value = "classpath:xx.properties",encoding = "UTF-8") public class URLNacosConfiguration2 { private Map<String, String> defaultMap; private List<String> defaultList; }
- 结果
最后
以上就是高挑老鼠最近收集整理的关于@Value和@ConfigurationProperties接收List、Map格式配置信息的2种方式的全部内容,更多相关@Value和@ConfigurationProperties接收List、Map格式配置信息内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复