认识yaml语法~

在这里插入图片描述
今天来学习springboot这个配置文件到底可以配置哪些东西

配置文件

SpringBoot使用一个全局的配置文件,配置文件名称是固定的

  • application.properties
    语法结构: key=vaiue
  • application.yaml
    语法结构: key:空格value

配置文件的作用∶修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了;

在这里插入图片描述

可以看到application.yaml所支持的语法是更强大的,但是对空格的格式要求是很高的。

用yaml文件赋值

  • 创建实体类
package com.lei.pojo;

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

@Component
public class Dog {
    
    
    @Value("旺财")
    private  String name;
    @Value("3")
    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 +
                '}';
    }
}

package com.lei.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 birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person() {
    
    
    }

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

    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 getBirth() {
    
    
        return birth;
    }

    public void setBirth(Date birth) {
    
    
        this.birth = birth;
    }

    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 +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

@ConfigurationProperties(prefix = “person”)
通过这个注解与yaml配置文件绑定

  • yaml赋值
person:
  name: lei
  age: 21
  happy: true
  birth: 1999/01/22
  maps: {
    
    k1: v1,k2: v2}
  lists:
    - music
    - girl
  dog:
    name: 旺财
    age: 3
  • 测试
package com.lei;

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

@SpringBootTest
class Springboot02ConfigApplicationTests {
    
    
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AIJXB/article/details/113525470

相关文章