如果key中存在特殊字符如:/、.、*等,我们可以使用"[]"进行转义
app:
"[/api/**]": anno
spring:
profiles:
active: dev
typechange:
turnOn: 1
java
/**
* 部署环境
*/
@Value("${spring.profiles.active:prod}")
private String env;
/**
* 1:开启 0:关闭
*/
@Value("${typechange.turnOn}")
private Integer turnOn;
test:
array1: aaa,bbb,ccc
array2: 111,222,333
array3: 11.1,22.2,33.3
java
// : 冒号后的值表示当 key 不存在时候使用的默认值,使用默认值时数组的 length = 0
@Value("${test.array1:}")
private String[] testArray1;
@Value("${test.array2:}")
private int[] testArray2;
@Value("${test.array3:}")
private double[] testArray3;
test:
list: aaa,bbb,ccc
java
// yml必须要有此配置,不然启动会报错
@Value("#{'${test.list}'.split(',')}")
private List testList;
//加默认值并判断集合非空,避免不配置这个 key 时候程序报错
@Value("#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}")
private List testList;
test:
set: 111,222,333,111
java
@Value("#{'${test.set:}'.empty ? null : '${test.set:}'.split(',')}")
private Set testSet;
// output: [111, 222, 333]
yml配置
test:
map1: '{"name": "zhangsan", "sex": "male"}'
map2: '{"math": "90", "english": "85"}'
Java
@Value("#{${test.map1}}")
private Map map1;
@Value("#{${test.map2}}")
private Map map2;
注意,使用这种方式,必须得在配置文件中配置该 key 及其 value。我在网上找了许多资料,都没找到利用 EL 表达式支持不配置 key/value 的写法。如果你真的很需要这个功能,就得自己写解析方法了,这里以使用 fastjson 进行解析为例:
(1) 自定义解析方法
public class MapDecoder {
public static Map decodeMap(String value) {
try {
return JSONObject.parseObject(value, new TypeReference
java使用
@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map1:}')}")
private Map map1;
@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map2:}')}")
private Map map2;
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps:
k1: v1
k2: 12
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12
varmaplist:
key11:
- t1
- t2
- t3
key22:
- t11
- t22
- t33
java用法:
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map maps;
private List lists;
private Dog dog;
private Map> varmaplist;
}
systems:
systemInfo:
"[http://test.mdm.bop.com]": #CMC
uiUrl: http://test.mdm.bop.com/cmc-portal
notLoginUrls: cmc-portal/api/**,cmc-portal/index,cmc-portal/logout
notAclUrls: cmc-portal/login,cmc-portal/link/rest/getMenuList,cmc-portal/link/rest/getRoleList
indexUrl: cmc-portal/index
logoutUrl: cmc-portal/logout
java 用法:
/**
* 系统公共配置
*
* @author zhaoyang10
* @date 2020/7/16
*/
@RefreshScope
@Component
@ConfigurationProperties(prefix = "systems")
@Data
public class NacosSystemConfig {
private Map systemInfo;
}
/**
* 系统配置项实体类
*
* @author zhaoyang10
* @date 2020/7/16
*/
@Data
public class SystemInfo {
/**
* 域名
*/
private String domain;
/**
* 前端url
*/
private String uiUrl;
/**
* 不需要登录的url
*/
private String notLoginUrls;
/**
* 需要登录但不需要鉴权的url
*/
private String notAclUrls;
/**
* 只需要weibo登录的url
*/
private String weiboLoginUrls;
/**
* 登录策略
*/
private String loginStrategy;
/**
* 鉴权url
*/
private String authRemoteUrl;
/**
* 登录url
*/
private String indexUrl;
/**
* 登出url
*/
private String logoutUrl;
/**
* cas/sso登录页url
*/
private String signoutUrl;
}
spring boot 配置文件详解 @ConfigurationProperties @Value区别