yaml 语法使用

1.添加Bean

package com.example.boot2.demo.bean;


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 = "emp")
public class Emp {
    private String lastName;
    private Integer age;
    private Double salary;
    private Boolean boss;

    @Override
    public String toString() {
        return "Emp{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", boss=" + boss +
                ", birthday=" + birthday +
                ", map=" + map +
                ", list=" + list +
                ", forte=" + forte +
                '}';
    }

    private Date birthday;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public com.example.boot2.demo.bean.forte getForte() {
        return forte;
    }

    public void setForte(com.example.boot2.demo.bean.forte forte) {
        this.forte = forte;
    }

    private Map map;
    private List list;

    private forte forte;


}

我们再定义   forte   这个类

package com.example.boot2.demo.bean;

public class forte {
    private String name;

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

    public String getName() {
        return name;
    }

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

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    private Integer time;

}

2. 在 application.yaml 添加配置信息

#emp 数据
emp:
  lastName: zxw
  age: 25
  salary: 10000
  boss: true
  birthday: 1995/10/10
  map:
    kay1: value1,
    key2: value2
  list:
    -one
    -two
    -three
  forte:
    name: java
    time: 3

3。添加一个测试类进行测试

package com.example.boot2.demo;

import com.example.boot2.demo.bean.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    Emp emp;
    @Test
    void contextLoads() {
        System.out.println(emp);
        //Emp{lastName='zxw', age=25, salary=10000.0, boss=true, birthday=Tue Oct 10 00:00:00 CST 1995, map={kay1=value1,, key2=value2}, list=[-one -two -three], forte=forte{name='java', time=3}}
    }

}

输出:

Emp{lastName='zxw', age=25, salary=10000.0, boss=true, birthday=Tue Oct 10 00:00:00 CST 1995, map={kay1=value1,, key2=value2}, list=[-one -two -three], forte=forte{name='java', time=3}}

posted @ 2020-02-29 13:20  1点  阅读(282)  评论(0编辑  收藏  举报