Configuration file of Spring Boot configuration (configuration file, loading order, configuration principle)

Keywords: Java Spring xml JSON

configuration file

Spring Boot uses a global configuration file whose name is fixed

  • application.yml

Function of configuration file: modify the default value of Spring Boot auto configuration, that is, modify the value that Spring Boot has configured for us at the bottom

YAML(YAML Ain't Markup Language)

Markup Language:

Previous configuration files; most used xxx.xml Documents;

YAML: data centric, more suitable for configuration files than json and xml

YAML configuration example (space must be added after colon)

  port: 8081

XML: Configuration Example


1.YAML syntax

1. Basic grammar

The relationship between attributes and values: k: (space) v: indicates a pair of key value pairs (space cannot be omitted);

Control hierarchy by indenting spaces: all left aligned data are at the same level

   port: 8081
   path: /hello

Attributes and values are also case sensitive;

2. Description of value

Literal: normal value (number, string, Boolean)

k: Write directly in the V word;

In general, strings do not need to be enclosed with single quotation marks or double quotation marks;

"Double quotation mark": it will escape the special characters in the string, and the special characters will be taken as the meaning it wants to identify

name: "zhangsan /n list"
Output: zhangsan linefeed list

'single quotation mark': special characters will not be escaped. The special characters are only common string data

name: 'zhangsan /n list'
//Output: zhangsan /n list

Object, Map (property and value) (key value pair):

k: v writes the relationship between the property and value of the object on the next line; pay attention to using space indentation

  lastName: zhangsan
  age: 20

In line:

friends: {lastName: zhangsan,age: 18}

Array (List, Set):

Represents an element in an array with a - (space) value

 - cat
 - dog
 - pig

In line writing

pets: {cat,dog,pig}

3. Profile value injection

1.@ConfigurationProperties (the default value is taken from the global configuration file)

configuration file

  port: 8081

  lastName: zhangsan
  age: 19
  boss: false
  birth: 2020/5/20
  maps: {k1: v1,k2: 12}
    - lisi
    - zhaoliu
    name: Wangcai
    age: 2


 * Map the value of each property configured in the configuration file to this component
 *  @ConfigurationProperties:Tell springBoot to bind all the properties in this class to the configuration related to the configuration file;
 *          prefix = "person":Which of the following attributes in the configuration file is mapped one by one
 *          Only when this component is a component in the container can the functions provided by the container be used;
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

After importing the profile processor, there will be a prompt when the profile is bound (the prompt in the configuration meets the switching of hump naming last name = last name)



     *  Spring The underlying Value, previously in the Spring configuration file
     *  <bean>
     *      <property name="lastName" value="Literal amount / ${key} - get value / {spel} "> < property / >
     *  </bean>
     *  Previously supported, supported in @ Value

    private String lastName;
    private Integer age;
    private Boolean boss;

3. Comparison between @ value and @ ConfigurationProperties

@ConfigutationProperties @Value
Batch injection support I won't support it
Loose binding (loose syntax: lastName=last_name=last-name) support I won't support it
SpEL I won't support it support
JSR303 data verification support I won't support it
Complex type encapsulation (object, map) support I won't support it

The configuration file can get the value whether it is ymal or properties


If you only get a Value in the configuration file in a business logic (Controller, Service), use @ Value

If we write a java bean to map with the configuration file, use @ ConfigurationProperties

4. Configuration file injection value data verification

@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;


  • @PropertySource: load the specified configuration file (if there is a matching prefix property in the global configuration file, the global file will be loaded first, and the file marked by PropertySource will not be found again)
@ConfigurationProperties(prefix = "person")
  • @Import resource: import the Spring configuration file to make the content in the configuration file effective;

There is no Spring configuration file in Spring Boot, and the configuration file we wrote ourselves cannot be recognized automatically;

To make the Spring configuration file effective, load it in; @ ImportResource is marked on a configuration class

@ImportResource(locations = {"classpath:beans.xml"})
Import Spring's configuration file for it to take effect
  • Recommended ways to add components in Spring Boot

    1. Do not write xml file, use full annotation method (configuration class = =) configuration file)

    2. Use @ Bean annotation to add components to the container

     * Indicates that the current class is a configuration class
     * Add components with bean s in the configuration file
     * Use @ Bean in configuration class
    public class MyAppConfig {
        //Add the return value of the method to the container: the default id of this component in the container is the method name
        public HelloService helloService(){
            System.out.println("@Bean Add build to container");
            return new HelloService();

4. Placeholder for configuration file

1. Random number


2. The placeholder gets the previously configured value. If it is not available, specify the default value

You can refer to the previously configured properties in the configuration file (all the previously configured properties of priority can be used here). If the properties cannot be found, use ${key: value} to specify the default value

person.last-name=Zhang San ${random.uuid}
person.boss=false${person.hello: hello}_dog


1. Multiple Profile files

When we write the main configuration file, the file name can be application-{profile}.properties/yml

Default use ;

2.yml supports multi document block mode

Three - can be divided into blocks

profiles: specify which environment (custom), active: activate which environment

  port: 8081
    active: prod

  port: 8082
  profiles: prod
  port: 8083
  profiles: dev

3. Activate the specified profile

1. In the default global configuration file activation

2. Command line activation:

  • cmd in the jar package directory: Java jar xxx.jar --

  • Program arguments of IDEA running configuration:--

  • VM options for IDEA running configuration:-

Posted by ManInBlack on Sat, 30 May 2020 02:51:32 -0700