SpringBoot three configuration files

Posted by WebDesEyen on Fri, 05 Nov 2021 23:33:04 +0100

SpringBoot uses a global configuration file, the application.properties configuration file. The path of this file is placed under the src/main/resources / directory or under the class path, and the file name is fixed. The IDEA tool is created under the corresponding first directory by default. Since it is a global configuration file, you can modify the global default configuration. In the actual development, we use more configuration files with yml suffix.

There are two formats for this configuration file

  • application.properties
  • application.yml

yaml and yml suffixes are OK

From the previous configuration of XML files, we can know that configuring data in XML will waste most of the information on the opening and closing of tags, and there are few really useful data. This is also the difference between XML and json data in the field of data transmission. In addition to being compatible with the configuration file type with the suffix of properties, springboot also has another way, yaml, which is very suitable for data centric configuration files. Next, let's introduce it.

1. yml file type

1.1 basic grammar

The basic syntax rules for configuration files with yml suffix are as follows

  • key: value; There is a space between kv

  • Case sensitive

  • Use indentation to represent hierarchical relationships

  • tab is not allowed for indentation, and only spaces are allowed (you can use it safely when developing with idea)

  • 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

1.2 data type

  • Literal: a single, non separable value. date,boolean,string,number,null
k: v     #The variable is called k and the value is v
  • Object: a collection of key value pairs. map,hash,set,object
#There are two ways to write it: the following two ways mean that there is a variable k, which has three attributes: k1, k2 and k3. The attribute values are V1, V2 and V3 respectively

Write in one line: 
k: {k1:v1,k2:v2,k3:v3}

#Or:
k: 
  k1: v1
  k2: v2
  k3: v3
  • Array: a set of values arranged in order. array,list,queue
    There are also two ways to write, both of which mean that the array has three values: V1, V2 and v3
Inline writing:  k: [v1,v2,v3]
#perhaps
k:
 - v1
 - v2
 - v3

1.3 example

Create a Person class and a Pet class

import lombok.Data;
import lombok.ToString;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ToString
@Data
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
-------------------------------------------------------------------------------
@ToString
@Data
public class Pet {
    private String name;
    private Double weight;
}

First add the @ Component annotation to the Person class into the container, and then add the @ ConfigurationProperties annotation to the Person class to bind it to the configuration file. Prefix indicates which prefix to bind to in the configuration file


Create an application.yml file in the resource directory

(in spring boot, application.properties and application.yml files can exist at the same time, and the configuration of both files will take effect)

Content of application.yml file: (pay attention to indentation)

# yaml represents the above objects
person:
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet: 
    name: tomcat
    weight: 23.4
  interests: [Basketball,Swimming]
  animal: 
    - jerry
    - mario
  score:
    english: 
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  allPets:
    sick:
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]

Get the person object in the main program for testing

   public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(HelloBootApplication.class, args);
        Person person = (Person) run.getBean("person");
        System.out.println(person);
    }

2. Configuration tips

There is generally no prompt for custom class and configuration file binding. If you want to prompt, you can add the following dependencies:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
  </dependency>

Because this is just a tip we developed. Although it has no impact on the function when packaged, it will always affect the performance. Therefore, we suggest that you do not package it when packaging. If you do not want to package it, you only need to add the following contents into the pom file

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Topics: Java Spring Boot Back-end