yaml语法详解

yaml语法详解_第1张图片

#k=v
#对空格的严格要求十分高
#注入到我们的配置类中
#普通的key=value
name: qinjiang

#对象
student:
 name: qingjiang
 age: 3

#行内写法
student1: {name: qinjiang,age: 3}

#数组
pets:
  - cat
  - dog
  - pig

pet: [cat,dog,pig]

 yaml语法详解_第2张图片

 yaml可以给实体类赋值

yaml语法详解_第3张图片

person:
  name: kuangshen
  age: 19
  happy: false
  bitth: 2023/07/23
  maps: {k1: v1,k2: v2}
  lists:
    - sing
    - dance
    - girl
  dog:
    name: 旺财
    age: 19



 

package com.kuang.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 bitth;
    private Map maps;
    private List lists;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date bitth, Map maps, List lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.bitth = bitth;
        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 getBitth() {
        return bitth;
    }

    public void setBitth(Date bitth) {
        this.bitth = bitth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", bitth=" + bitth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
 
  

 yaml语法详解_第4张图片

 

package com.kuang.pojo;

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

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
//加载指定配置文件
//javaConfig 绑定我们配置文件的值,可以采取这些方式!
//@PropertySource(value = "classpath:qingjiang.properties")
public class Person {
    //spEL表达式取值 ${key}
   // @Value("${name}")
    private String name;
    private Integer age;
    private Boolean happy;
    private Date bitth;
    private Map maps;
    private List lists;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date bitth, Map maps, List lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.bitth = bitth;
        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 getBitth() {
        return bitth;
    }

    public void setBitth(Date bitth) {
        this.bitth = bitth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", bitth=" + bitth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
 
  
name=基哥
package com.kuang;

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

import java.util.Date;

@SpringBootTest
class Springboot02ConfigApplicationTests {
  //自动装配,先根据容器里的组件类型匹配,在根据id,若没有匹配的,可以用@Qualifier(value="xxx");
    @Autowired
  private Person dog;
    @Test
    void contextLoads() {
        System.out.println(dog);
    }

}

yaml语法详解_第5张图片

 

你可能感兴趣的:(SpringBoot,windows,spring,boot)