springboot(2)-yaml configuration injection

1 yaml configuration injection

The more powerful yaml file is that it can directly inject matching values ​​into our entity classes.
SpringBoot uses a global configuration file, and the configuration file name is fixed **[application.yaml]**
Insert picture description here
The role of the configuration file : modify SpringBoot automatic The default value of the configuration.

Basic grammar of yaml
Description: 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.
Example
Insert picture description here
program
Insert picture description here
Dog.java

package com.zs.helloword.pojo;

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

@Component
public class Dog {
    
    
    private String name;
    private Integer age;

    public Dog() {
    
    
    }

    public Dog(String name, Integer age) {
    
    
        this.name = name;
        this.age = age;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Person.java

package com.zs.helloword.pojo;

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 brith;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person(String name, Integer age, Boolean happy, Date brith, Map<String, Object> maps, List<Object> lists, Dog dog) {
    
    
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.brith = brith;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public Person() {
    
    
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    public Boolean getHappy() {
    
    
        return happy;
    }

    public void setHappy(Boolean happy) {
    
    
        this.happy = happy;
    }

    public Date getBrith() {
    
    
        return brith;
    }

    public void setBrith(Date brith) {
    
    
        this.brith = brith;
    }

    public Map<String, Object> getMaps() {
    
    
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
    
    
        this.maps = maps;
    }

    public List<Object> getLists() {
    
    
        return lists;
    }

    public void setLists(List<Object> lists) {
    
    
        this.lists = lists;
    }

    public Dog getDog() {
    
    
        return dog;
    }

    public void setDog(Dog dog) {
    
    
        this.dog = dog;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", brith=" + brith +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

HellowordApplicationTests.java

package com.zs.helloword;

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

// 单元测试
@SpringBootTest
class HellowordApplicationTests {
    
    
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

application.yaml

person:
  name: zs
  age: 11
  dog:
    name: zm
    age: 3

print

Person{
    
    name='zs', age=11, happy=null, brith=null, maps=null, lists=null, dog=Dog{
    
    name='zm', age=3}}

also

person:
  name: qinjiang${
    
    random.uuid} # 随机uuid
  age: ${
    
    random.int}  # 随机int
  happy: false
  birth: 2000/01/01
  maps: {
    
    k1: v1,k2: v2}
  lists:
    - code
    - girl
    - music
  dog:
    name: ${
    
    person.hello:other}_旺财
    age: 1
Person{
    
    name='qinjiang448afe0c-99ff-40ae-b5a0-3041c214aabc', age=1523578612, happy=false, brith=null, maps={
    
    k1=v1, k2=v2}, lists=[code, girl, music], dog=Dog{
    
    name='other_旺财', age=1}}

Special attention is

@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应

In addition, configure the dependency in the pom.xml file

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

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112417906