SpringBoot study notes 3-yaml configuration injection

copy notes from mad god original article addresses

yaml grammar learning

Configuration file

SpringBoot uses a global configuration file, and the configuration file name is fixed (in the following two ways)

  • application.properties

    Grammatical structure: key=value

  • application.yml

    Syntax structure: key: space value
    The role of the configuration file : modify the default value of SpringBoot automatic configuration, because SpringBoot automatically configures us at the bottom;

For example, we can modify the port number that Tomcat starts by default in the configuration file! have a test!

server.port=8081 

yaml overview

YAML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). When developing this language, YAML actually means: "Yet Another Markup Language" (still a markup language)

This language is data-centric, not markup languages!

Most of the previous configuration files are configured using xml; for example, a simple port configuration, let's compare the
traditional xml configuration of yaml and xml :

<server>
    <port>8081<port>
</server>

yaml configuration:

server:
  prot: 8080

yaml basic grammar

Note: The grammar is strict!

1. Spaces cannot be omitted

2. Use indentation to control the hierarchical relationship, as long as the column of data aligned on the left is at the same level.

3. The case of attributes and values ​​is very sensitive.

Literal: ordinary value [number, boolean, string]

The literal can be written directly behind, and the string does not need to be enclosed in double quotes or single quotes by default ;

k: v
Note:

"" Double quotation marks will not escape the special characters in the string, and the special characters will be what they want to express;

For example: name: "kuang \n shen" Output: kuan, line feed shen

'' Single quotation marks will escape special characters, and special characters will eventually become output like ordinary characters

For example: name:'kuang \n shen' output: kuang \n shen

Object, Map (key-value pair)

#Object, Map format

k: 
    v1:
    v2:

Write the properties and worth relationships of the object on the next line, paying attention to indentation; for example:

student:
    name: qinjiang
    age: 3

Inline writing

student: {name: qinjiang,age: 3}

Array (List, set)

Use-value to represent an element in the array, for example:

pets:
 - cat
 - dog
 - pig

Inline writing

pets: [cat,dog,pig]

Modify the default port number of SpringBoot

Add the parameter of the port number in the configuration file, you can switch the port;

server:
  port: 8082

Inject the configuration file

The more powerful part of the yaml file is that it can directly inject matching values ​​into our entity classes!

yaml injection configuration file

1. Create a new file application.yml in the resources directory of the springboot project to
write content

person:
  name: zhouyi
  age: 20
  happy: false
  birth: 2021/3/13
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - music
    - girl
  dog:
    name: 旺财
    age: 3

2. Write an entity class People and add it to the class;

@Component
@ConfigurationProperties(prefix = "person")
package com.zhou.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    
    
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;

    private Dog dog;
    .....

In the test class

package com.zhou;

import com.zhou.pojo.Dog;
import com.zhou.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ConfigsApplicationTests {
    
    
    @Autowired
   private Dog dog;
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
    
    
        System.out.println(dog);
        System.out.println(person);

    }

}

The results of the operation are:
Insert picture description here

Use the properties file to assemble the properties of the object

1. Create a new dog.properties file
Insert picture description here

2. Create a new dog entity class

package com.zhou.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component

//加载指定的配置文件
@PropertySource(value = "classpath:dog.properties")
public class Dog {
    @Value("${name}")
    private  String name;
    @Value("${age}")
    private Integer age;

Insert picture description here
3. Carry out the test
Insert picture description here
Run results:
Insert picture description here

Comparison summary

@Value is not friendly to use! We need to annotate and assign a value to each property separately, which is more troublesome; let’s look at a function comparison chart
Insert picture description here
1. @ConfigurationProperties only needs to be written once, and @Value needs to be added for each field

2. Loose binding: What does this mean? For example, the last-name written in my yml is the same as lastName.-The letters that follow are capitalized by default. This is loose binding. Can test it

3. JSR303 data verification, this is that we can add a layer of filter verification in the field to ensure the legality of the data

4. Encapsulation of complex types. Objects can be encapsulated in yml, but value is not supported

in conclusion:

Both configuration yml and configuration properties can get the value, yml is strongly recommended;

If we are in a business and only need to get a certain value in the configuration file, we can use @value;

If we say that we have specially written a JavaBean to map one-to-one with the configuration file, just directly @configurationProperties, don't hesitate!

Guess you like

Origin blog.csdn.net/qq_44788518/article/details/114761125