Table of contents
2.1 Literals (ordinary values: numbers, strings, Boolean)
1 Grammar
- k: (space) v: represents a key-value pair (space must be present)
- Case Sensitive
- Use indentation to indicate hierarchical relationships
- Tabs are not allowed for indentation, only spaces are allowed.
- The number of spaces for indentation does not matter, as long as elements at the same level are left aligned
- # represents comments
- The string does not need to be quoted. If '' single quotes or "" double quotes are added, the content will be escaped [single quote escape] or not escaped [double quote not escaped]
2 Configuration information
2.1 Literals (ordinary values: numbers, strings, Boolean)
Written: k: v
Strings do not need to be enclosed in single quotes or double quotes by default;
"": double quotes; special characters in the string will not be escaped; special characters will be used as the meaning they want to express.
name: "zhangsan \n lisi": output; zhangsan newline lisi
'': single quote; will escape special characters, which are ultimately just ordinary string data.
name: 'zhangsan \n lisi': output; zhangsan \n lisi
server:
port: 8080
servlet:
context-path: /
method of obtaining:
@Value("${server.port}")
private Integer port;
2.2 Objects
Writing method: k: v, write the relationship between the object's attributes and values on the next line; pay attention to the indentation
example:
#对象行内写法
school: '{"name": "北京市第一中学","location": "北京市"}'
school2:
name: 北京市第一中学
location: 北京市
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private School school;
private School school2;
}
2.3 Map
Writing method: k: v, write the relationship between the object's attributes and values on the next line; pay attention to the indentation
- Inline writing and how to obtain it
map:
example: '{"key1": "value1", "key2": "value2"}'
@Component
public class TestConfig {
@Value("#{${map.example}}")
private Map<String, String> map;
}
- Offline writing and how to obtain it
example:
map:
key1: value1
key2: value2
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private Map<String, String> map;
}
2.4 List
- Outline writing method (use - value to represent an element in the array) and how to obtain it
example:
list:
- list1
- list2
- list3
@Data
@Component
@ConfigurationProperties(prefix = "example")
public class TestConfig2 {
private List<String> list;
}
- Inline writing and how to obtain it
list:
# 行内写法
example: list1,list2,list3
@Component
public class TestConfig {
// @Value("#{'${list.example2}'.empty ? null : '${list.example2}'.split(',')}")
@Value("#{'${list.example}'.split(',')}")
private List<String> list;
}
2.5 Array
array:
example: array1,array2,array3
@Component
public class TestConfig {
@Value("${array.example}")
private String[] array;
}
3 Summary
type |
Configuration method |
Value method |
literal |
key: value |
@Value("${key}") |
object |
example: object: '{"key1": "value1","key2": "value2"}' |
@ConfigurationProperties(prefix = "example") public class TestConfig2 { private Object object; } |
example: object: key1: value1 key2: value2 |
||
array |
example: array: array1,array2,array3 |
@Value("${example.array}") |
List |
example: list: - sheet 1 - list2 -list3 |
@ConfigurationProperties(prefix = "example") public class TestConfig2 { private List<String> 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<String, String> map; } |