SpringBoot--yml/yaml configuration file

Posted by Mr_Pancakes on Thu, 17 Feb 2022 10:56:55 +0100


In addition to the properties configuration file, spring boot can also use yaml configuration file, which is more intuitive.

brief introduction

YAML is not an acronym for recursive markup language. When developing this language, YAML actually means "Yet Another Markup Language".

It is very suitable for data centric configuration files


Basic grammar

  • key: valueï¼› There is a space between kv
  • Case sensitive
  • Use indents to indicate hierarchical relationships
    • tab is not allowed for indentation, only spaces are allowed
    • The number of indented spaces is not important, as long as the elements of the same level are aligned to the left
  • #: indicates a comment
  • There is no need to quote the string. If you want to add, '' and '' means that the string content will be escaped / not escaped

data type


Create a new SpringBoot project

In POM The Lombok dependency is introduced into the XML configuration file.

Create a new bean folder in src/main/java/boot directory, and write Pet and Person classes:

Pet class:

import lombok.Data;
import lombok.ToString;

public class Pet {

    private String name;
    private Double weight;

Person class:

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "person")
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Pet pet;
    private Map<String, List<Pet>> allPets;

@ConfigurationProperties(prefix = "person"): identifies the class in the configuration file

Create a new application in src/main/resources directory Yaml configuration file (take configuring Person class as an example):

  userName: zhangsan
  boss: true
  birth: 2020/12/9
  age: 18
    - Basketball
    - Football
  animal: [cat,dog]
    english: 80
    math: 90
  salarys: [9999.98, 9999.99]
    name: Dog
    weight: 32.34
      - {name: dog, weight: 12.23}
      - name: cat
        weight: 9.32
      - name: pig
        weight: 100.98
    health: [{name: cattle, weight: 128.23},{name: sheep, weight: 124.23}]

You should pay attention to the writing of pet and allPets attributes

Test, create a new controller folder in src/main/java/boot directory, and write HelloController class:

import boot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {

    Person person;

    public Person person() {
        String userName = person.getUserName();
        return person;

Start the service, browser access: http://localhost:8080/person

be careful

Quotation mark wrapping problem of string

Questions about strings wrapped by '' and '':

Single quotation mark, escape

  userName: 'zhangsan \n lisi'

Double quotation marks, no escape

  userName: "zhangsan \n lisi"

How to understand escape and non escape: Originally \ n means line feed. When wrapped in single quotation marks, it will be escaped, that is, the newline will be invalid; Double quotation mark wrapping does not escape, that is, it retains the original line feed effect.

properties and priority of yaml and yml

When the configuration files of properties, yaml and yml are written at the same time, and the contents are in conflict, the loading order is:

yml > yaml > properties

Since the later loaded content will overwrite the first loaded content, in terms of display effect, the order is as follows:

properties > yaml > yml

Example, in application Properties and application Configure userName in yaml configuration file at the same time:




  userName: zhangsan

After operation, the effect is as follows:


Syntax tips for yaml configuration files

There is no automatic syntax prompt when writing yaml configuration file:

Need to be in POM The following dependencies are introduced into the XML configuration file:





Indicates a syntax hint


Represents a dependency that does not add syntax hints when packaging


explain: person. User name: - N means capital n


Using yml/yaml configuration file is more convenient for visual display and understanding.