目录
1 语法
2 配置信息
2.1 字面量(普通的值:数字,字符串,布尔)
2.2 对象
2.3 Map
2.4 List
2.5 数组
3 总结
写法:k: v
字符串默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
server:
port: 8080
servlet:
context-path: /
获取方式:
@Value("${server.port}")
private Integer port;
写法:k: v,在下一行来写对象的属性和值的关系;注意缩进
example:
#对象行内写法
school: '{"name": "北京市第一中学","location": "北京市"}'
school2:
name: 北京市第一中学
location: 北京市
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private School school;
private School school2;
}
写法:k: v,在下一行来写对象的属性和值的关系;注意缩进
map:
example: '{"key1": "value1", "key2": "value2"}'
@Component
public class TestConfig {
@Value("#{${map.example}}")
private Map map;
}
example:
map:
key1: value1
key2: value2
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private Map map;
}
example:
list:
- list1
- list2
- list3
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private List list;
}
list:
# 行内写法
example: list1,list2,list3
@Component
public class TestConfig {
// @Value("#{'${list.example2}'.empty ? null : '${list.example2}'.split(',')}")
@Value("#{'${list.example}'.split(',')}")
private List list;
}
array:
example: array1,array2,array3
@Component
public class TestConfig {
@Value("${array.example}")
private String[] array;
}
类型 |
配置方式 |
取值方式 |
字面量 |
key: value |
@Value("${key}") |
对象 |
example: object: '{"key1": "value1","key2": "value2"}' |
@ConfigurationProperties(prefix = "example") public class TestConfig2 { private Object object; } |
example: object: key1: value1 key2: value2 |
||
数组 |
example: array: array1,array2,array3 |
@Value("${example.array}") |
List |
example: list: - list1 - list2 - list3 |
@ConfigurationProperties(prefix = "example") public class TestConfig2 { private List } |
example: list: list1,list2,list3 |
@Value("#{'${list.example2}'.empty ? null : '${list.example2}'.split(',')}") @Value("#{'${list.example}'.split(',')}") |
|
Map |
map: example: '{"key1": "value1", "key2": "value2"}' |
@Value("#{${map.example}}") |
example: map: key1: value1 key2: value2 |
@ConfigurationProperties(prefix = "example") public class TestConfig2 { private Map } |