Source: blog.csdn.net/chenlixiao007
1. What is YAML
YAML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). YAML actually means: "Yet Another Markup Language" (still a markup language). The main strength of this kind of speech is data-centric rather than markup language. For example, xml language will use a lot of markup.
YAML is a highly readable and easy-to-understand format used to express data serialization. Its syntax is similar to other high-level languages, and it can simply express data forms such as lists (arrays), hash tables, scalars, etc. It uses blank symbol indentation and a large number of appearance-dependent features, and is particularly suitable for expressing or editing data structures, various configuration files, and so on.
The suffix of the YAML configuration file is .yml, such as the configuration file application.yml used in the Springboot project.
2. Basic grammar
- YAML uses printable Unicode characters, UTF-8 or UTF-16 can be used.
- The data structure is in the form of key-value pairs, namely key name: value. Note that there must be a space after the colon.
- Each list (array) member is represented by a single line, and starts with a dash + blank (-). Or use square brackets ([]) and separate members with commas + blanks (, ).
- Each member of the hash table uses a colon+blank (:) to separate the key value and the content. Or use curly braces ({ }) and separate them with commas + blanks (, ).
- String values generally do not use quotation marks, but can be used when necessary. When double quotation marks are used to represent a string, special characters in the string (such as \n) will be escaped. When single quotes are used, special characters in the string are not escaped.
- Case sensitive 0d9f12e0ed43f02a97c0941c5d325d989c6af5fb0276dc7&scene=21#wechat_redirect)
- Use indentation to indicate the hierarchical relationship. Tabs are not allowed for indentation, only spaces are allowed, because the length of tabs may be different under different systems
- The number of spaces for indentation can be arbitrary, as long as the elements of the same level are aligned to the left
- In a single file, three consecutive hyphens (—) can be used to distinguish multiple files. There are also optional consecutive three dots (...) to indicate the end of the file.
- '\#' means comment, which can appear anywhere in a line, single-line comment
- When using commas and colons, a blank character must be followed, so you can freely add separators (for example: 5,280 or http://www.wikipedia.org) in the string or value without using quotation marks.
3. Data type
- Scalars: a single, indivisible value
- Object: a collection of key-value pairs, also known as mapping/hashes/dictionary
- Array: A set of values arranged in order, also known as sequence/list
scalar
Scalar is the most basic data type, indivisible value, they are generally used to represent a single variable, there are the following seven types:
- [String
- [Boolean value]
- [Integer]
- [Floating point number]
- [Null]
- [time]
- [date]
# 字符串 string.value: Hello!我是陈皮!
# 布尔值,true或false boolean.value: true boolean.value1: false
# 整数 int.value: 10 int.value1: 0b1010_0111_0100_1010_1110
# 二进制 # 浮点数 float.value: 3.14159 float.value1: 314159e-5
# 科学计数法 # Null,~代表null null.value: ~
# 时间,时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区 datetime.value: !!timestamp 2021-04-13T10:31:00+08:00
# 日期,日期必须使用ISO 8601格式,即yyyy-MM-dd date.value: !!timestamp 2021-04-13
In this way, we can introduce it in the program as follows:
@RestController @RequestMapping("demo") public class PropConfig { @Value("${string.value}") private String stringValue; @Value("${boolean.value}") private boolean booleanValue; @Value("${boolean.value1}") private boolean booleanValue1; @Value("${int.value}") private int intValue; @Value("${int.value1}") private int intValue1; @Value("${float.value}") private float floatValue; @Value("${float.value1}") private float floatValue1; @Value("${null.value}") private String nullValue; @Value("${datetime.value}") private Date datetimeValue; @Value("${date.value}") private Date datevalue; }
The basics of Spring Boot will not be introduced. I recommend this practical tutorial: https://github.com/javastacks/spring-boot-best-practice
object
We know that a single variable can be a key-value pair, using a colon structure to represent key: value. Note that a space should be added after the colon. You can use indentation level key-value pairs to represent an object, as shown below:
person:
name: 陈皮
age: 18
man: true
Then in the program, assign these attributes to the Person object. Note that the Person class should add get/set methods, otherwise the attributes will not be able to get the value of the configuration file correctly. Using @ConfigurationProperties to inject objects, @value cannot parse complex objects well.
@Configuration
@ConfigurationProperties(prefix = "my.person")
@Getter
@Setter
public class Person {
private String name;
private int age;
private boolean man;
}
Of course, the form of key:{key1: value1, key2: value2, ...} can also be used, as follows:
person: {name: 陈皮, age: 18, man: true}
array
You can use a short bar and a space-the beginning of the line to form each element of the array, the address field is as follows: Spring Boot study notes shared with you.
person:
name: 陈皮
age: 18
man: true
address:
- 深圳
- 北京
- 广州
You can also use square brackets for inline display, as follows:
person:
name: 陈皮
age: 18
man: true
address: [深圳, 北京, 广州]
The way to introduce in the code is as follows:
@Configuration
@ConfigurationProperties(prefix = "person")
@Getter
@Setter
@ToString
public class Person {
private String name;
private int age;
private boolean man;
private List<String> address;
}
If the members of the array field are also an array, you can use the nested form, as follows:
person:
name: 陈皮
age: 18
man: true
address: [深圳, 北京, 广州]
twoArr:
-
- 2
- 3
- 1
-
- 10
- 12
- 30
@Configuration
@ConfigurationProperties(prefix = "person")
@Getter
@Setter
@ToString
public class Person {
private String name;
private int age;
private boolean man;java
private List<String> address;
private List<List<Integer>> twoArr;
}
If the array member is an object, it uses the following two forms:
childs:
-
name: 小红
age: 10
-
name: 小王
age: 15
childs: [{name: 小红, age: 10}, {name: 小王, age: 15}]
4. Text block
If you want to introduce a multi-line text block, you can use the | symbol, pay attention to the space between the colon: and | symbol. The latest interview questions have been sorted out, click on the Java Interview Library applet to refresh the questions online.
person:
name: |
Hello Java!!
I am fine!
Thanks! GoodBye!
It has the same effect as adding double quotation marks. Double quotation marks can escape special characters:
person:
name: "Hello Java!!\nI am fine!\nThanks! GoodBye!"
5. Display the specified type
Sometimes we need to display the type of certain values, we can use! (Exclamation mark) to explicitly specify the type. ! A single exclamation mark is usually a custom type, and a double exclamation mark is a built-in type, for example:
# 指定为字符串
string.value: !!str HelloWorld!
# !!timestamp指定为日期时间类型
datetime.value: !!timestamp 2021-04-13T02:31:00+08:00
The built-in types are as follows:
- !!int: integer type
- !!float: floating point type
- !!bool: Boolean type
- !!str: string type
- !!binary: Binary type
- !!timestamp: Date and time type
- !!null: null value
- !!set: collection type
- !!omap, !!pairs: key value list or object list
- !!seq: sequence
- !!map: hash table type
6. Quote
Reference will use the & anchor point coincidence and asterisk symbols, & is used to establish an anchor point, and << means merge into the current data and is used to reference the anchor point. The latest interview questions have been sorted out, click on the Java Interview Library applet to refresh the questions online.
xiaohong: &xiaohong
name: 小红
age: 20
dept:
id: D15D8E4F6D68A4E88E
<<: *xiaohong
The above is ultimately equivalent to the following:
xiaohong:
name: 小红
age: 20
dept:
id: D15D8E4F6D68A4E88E
name: 小红
age: 20
There is also a kind of in-file reference, which refers to the variables that have been defined, as follows:
base.host: https://chenpi.com
add.person.url: ${base.host}/person/add
7. Single file multiple configuration
Multiple document partitions can be implemented in the same file, that is, multiple configurations. In a yml file, pass — to separate multiple different configurations, and decide which configuration to enable according to the value of spring.profiles.active
#公共配置
spring:
profiles:
active: pro # 指定使用哪个文档块
#开发环境配置
spring:
profiles: dev # profiles属性代表配置的名称
server:
port: 8080
#生产环境配置
spring:
profiles: pro
server:
port: 8081
recommends 2 original springboot+Vue projects, with complete video explanation, documentation and source code:
【VueAdmin】Teach you how to develop SpringBoot+Jwt+Vue's front-end and back-end separation management system
- Video explanation: https://www.bilibili.com/video/BV1af4y1s7Wh/
- Full development document front end: https://www.zhuawaba.com/post/18
- Full development document backend: https://www.zhuawaba.com/post/19
[VueBlog] Complete teaching of front-end and back-end separation blog project based on SpringBoot+Vue development
- Video explanation: https://www.bilibili.com/video/BV1af4y1s7Wh/
- Complete development document: https://www.zhuawaba.com/post/17
If you have any questions, come to my public account [161c987abcd871 Java Q & A ] to ask me
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。