Basic syntax of yml configuration file in springboot

  1. There are two types of configuration files in springboot, one is application.properties [and] the other is application.yml. These two configuration files are the global configuration files of springboot, and the configuration file names are fixed. We can By modifying the default configuration in the bottom layer of springboot in these two configuration files to meet our specific needs, and we are familiar with application.properties, the following is about the relevant content and syntax of the yml configuration file

  2. ① Most of the configuration files we see in the spring project end with xml files, and modifying the configuration in the xml file is often based on tags, while the yml configuration file is different. It is based on data. The central configuration file has less complicated label configuration, and directly represents the corresponding value with data, so it will be better than the traditional xml file, and it is more suitable for the configuration file

② The following is the basic syntax of the yml configuration file:

a) Represented in the form of key-value pairs

k: v represents a key-value pair ( there must be a space between the colon and the value )

The hierarchical relationship is controlled by the indentation of spaces. As long as it is a left-aligned column of data, it is of the same level. Attributes and values ​​are case-sensitive.

server:
    port:8080
    path:/hello

b) how to write the value

Literals: ordinary values ​​(numbers, strings, booleans)

k: v literals are written directly

Strings do not need single or double quotes by default

“”: Double quotes do not escape special characters in the string

name: “zhangsan \n lisi” Output: zhangsan newline lisi

”: single quotes will escape special characters in the string

name: ‘zhangsan \n lisi’ Output: zhangsan \n lisi

Object, Map (key-value pair)

k: v Write the relationship between the attributes and values ​​of the object in the next line, you need to pay attention to the problem of indentation

The object still uses the k:v method

friends:
    name: zhangsan
    age:20

Inline writing:

pets:
    - cat
    - dog
    - pig

Arrays (List, Set)

Use – to represent an element in an array

pets:
    - cat
    - dog
    - pig

Inline writing:

pets:[cat, dog, pig]

Leave a Comment

Your email address will not be published. Required fields are marked *