Source: blog.csdn.net/chenlixiao007
1. What is YAML
YAML is a recursive abbreviation of “YAML Ain’t a Markup Language” (YAML is not a Markup Language). YAML actually stands for “Yet Another Markup Language.” Primary strength This kind of speech is data-centric rather than markup language, such as XML, which uses a lot of markup.
YAML is a readable, easy-to-understand format for expressing data serialization. Its syntax is similar to other high-level languages, and it can easily express lists (arrays), hash tables, scalars, and other data forms. Its use of whitespace indentation and a number of look-and-feel features makes it particularly suitable for expressing or editing data structures, various configuration files, and so on.
The extension of the YAML configuration file is.yml, for example, the application.yml configuration file used in the Springboot project.
2. Basic grammar
- YAML uses printable Unicode characters, which can be UTF-8 or UTF-16.
- The data structure takes the form of key-value pairs, namely key names: values, with Spaces after colons.
- Each list (array) member is represented on a single line, starting with a dash + blank (-). Or use square brackets ([]) and separate the members with a comma + blank (,).
- Each hash table member is separated by a colon + whitespace (:). Or use braces ({}) separated by a comma + blank (,).
- You can use quotation marks when necessary. When double quotation marks are used to represent a string, special characters (for example, \n) in the string are escaped. Special characters in strings are not escaped when single quotes are used.
- Case-sensitive 0 d9f12e0ed43f02a97c0941c5d325d989c6af5fb0276dc7 & scene = 21 # wechat_redirect)
- Use indentation to indicate hierarchy. Only Spaces are allowed for indentation, not tabs, because tabs may have different lengths on different systems
- You can indent any number of Spaces as long as elements of the same rank are left aligned
- In a single file, three consecutive hyphens (-) can be used to distinguish multiple files. And optionally three dots in a row (…) Indicates the end of a file.
- The ‘#’ indicates a comment, which can appear anywhere on a line, a single line comment
- When commas and colons are used, they must be followed by a whitespace character, so you can add delimiters (for example, 5,280 or www.wikipedia.org) to strings or numbers without using quotation marks.
3. Data types
- Scalar quantities (scalars) : single, non-separable values
- Object: a collection of key-value pairs, also known as mapping/hashes/dictionary
- 1. An ordered set of values, also called a sequence/list.
scalar
Scalars are the most basic data types, non-divisible values. They are generally used to represent individual variables. There are seven of them:
- [string
- Boolean value
- [integer]
- [Floating point]
- [Null]
- [time]
- [date]
# string string. Value: Hello! I am tangerine peel! Value: true Boolean. Value1: false # Integer int. Value: 10 int. Value1: 0b1010_0111_0100_1010_1110 # binary # float. Value: 3.14159 float. ~ # datetime.value:!! Timestamp 2021-04-13T10:31:00+08:00 # Date, date must be in ISO 8601 format, yyyY-MM-DD date.value:!! timestamp 2021-04-13Copy the code
Thus, we can introduce 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; }
Copy the code
Spring Boot foundation is not introduced, recommend the actual combat tutorial: github.com/javastacks/…
object
We know that a single variable can be used as a key-value pair, using the colon structure for key: value, with a space after the colon. An object can be represented with an indented key-value pair, as follows:
Person: name: orange peel age: 18 man: trueCopy the code
The Person class must use get/set methods, otherwise the property will fail to fetch the value from the configuration file. Using @ConfigurationProperties to inject objects, @Value does not parse complex objects very well.
@Configuration @ConfigurationProperties(prefix = "my.person") @Getter @Setter public class Person { private String name; private int age; private boolean man; }Copy the code
Key :{key1: value1, key2: value2,… }, as follows:
Person: {name: orange peel, age: 18, man: true}Copy the code
An array of
You can use a bar to form each element of the array with a blank – leading line, as shown in the address field: Spring Boot Learning Notes to share with you.
Person: name: Tangerine peel age: 18 man: true Address: - Shenzhen - Beijing - GuangzhouCopy the code
You can also use brackets for inline display, as follows:
Person: name: Tangerine peel age: 18 man: true address: [shenzhen, Beijing, Guangzhou]Copy the code
This is introduced in code 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;
}
Copy the code
If the member of an array field is also an array, the nested form can be used as follows:
Person: Name: Tangerine peel Age: 18 man: True Address: [Shenzhen, Beijing, Guangzhou] twoArr: -- 2-3-1 -- 10-12-30Copy the code
@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; }Copy the code
If the array member is an object, it takes one of two forms:
Childs: - name: xiao Hong age: 10 - name: Xiao Wang age: 15Copy the code
Childs: [{name: xiaohong, age: 10}, {name: xiaowang, age: 15}]Copy the code
4. Text blocks
If you want to introduce more lines of text block, you can use | symbols, pay attention to in the colon, and to have space between the | symbols. The latest interview questions sorted, click on the Java interview library small procedures online brush questions.
person:
name: |
Hello Java!!
I am fine!
Thanks! GoodBye!
Copy the code
It has the same effect as double quotes, which escape special characters:
person: name: "Hello Java!! \nI am fine! \nThanks! GoodBye!"Copy the code
5. Display the specified type
Sometimes we need to display types that specify certain values, which can be used! (exclamation mark) Specifies the type explicitly. ! The single exclamation mark is usually a custom type. Double exclamation marks are built-in types, such as:
# specifies the string string.value:!! str HelloWorld! #!!!!! Timestamp specifies the datetime.value:!! timestamp 2021-04-13T02:31:00+08:00Copy the code
The built-in types are as follows:
- !!!!! Int: indicates an integer
- !!!!! Float: indicates the floating point type
- !!!!! Bool: Boolean type
- !!!!! STR: a string
- !!!!! Binary: indicates the binary type
- !!!!! Timestamp: Indicates the date and time type
- !!!!! Null: an empty value
- !!!!! Set: Indicates the set type
- !!!!! Omap,!!!!! Pairs: lists of keys or objects
- !!!!! Sequence of seq:
- !!!!! Map: hash table type
6, references,
The reference uses the ampersand anchor match and the asterisk symbol, the ampersand to create the anchor, and the << to merge into the current data to refer to the anchor. The latest interview questions sorted, click on the Java interview library small procedures online brush questions.
Xiaohong: &xiaohong name: xiaohong Age: 20 dept: ID: D15D8E4F6D68A4E88E <<: *xiaohongCopy the code
The above ultimately amounts to the following:
Xiaohong: name: red age: 20 Dept: ID: D15D8E4F6D68A4E88E Name: red age: 20Copy the code
There is also an in-file reference that refers to a defined variable as follows:
base.host: https://chenpi.com
add.person.url: ${base.host}/person/add
Copy the code
7. Single file with multiple configurations
Multiple document partitions, or multiple configurations, can be implemented in the same file. In a YML file, separate multiple different configurations based on the value of spring.profiles. Active to decide which configuration to enable
# Public configuration Spring: Profiles: Active: Pro # Specify which document block to use # development environment configuration Spring: Profiles: dev # Profiles property represents the name of the configuration Server: port: Spring: Profiles: Pro Server: port: 8081Copy the code
Recommend 2 original Springboot +Vue projects, with complete video explanation and documentation and source code:
【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system
- Video tutorial: www.bilibili.com/video/BV1af…
- Complete development document front end: www.zhuawaba.com/post/18
- Full development documentation backend: www.zhuawaba.com/post/19
【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching
- Video tutorial: www.bilibili.com/video/BV1af…
- Full development documentation: www.zhuawaba.com/post/17
If you have any questions, please come to my official account [Java Q&A Society] and ask me