- 引以为戒:指点江山(装逼)后请帮人解答疑惑(大佬的基本素养)
- 科普借鉴:https://cloud.tencent.com/developer/article/1522660
- @Value不支持复杂类型封装,但是我们可能有实际需要指定不同的全路径进行List、Map的接收(详见下文)。
- 属性默认值大家可以在评论区贴出来(赶需求中,临时记录一下)
- 配置信息(Nacos配置格式缩进用的Tab,复制需要修改缩进)
-
url:
-
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
-
package 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
-
package 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;
-
}
- 结果