springboot(2)——yaml配置注入

1 yaml配置注入

yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值
SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的**【application.yaml】**
在这里插入图片描述
配置文件的作用:修改SpringBoot自动配置的默认值。

yaml基础语法
说明:语法要求严格!
1、空格不能省略
2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。
3、属性和值的大小写都是十分敏感的。
例子
在这里插入图片描述
程序
在这里插入图片描述
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

打印

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

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}}

特别注意的是

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

另,在pom.xml文件中配置dependency

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

猜你喜欢

转载自blog.csdn.net/zs18753479279/article/details/112417906