springBoot2.0 Yaml值获取

1. pom.xml添加如下依赖

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

2. 编写bean文件

package cn.jfjb.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;

/**
 * @author john
 * @date 2019/11/22 - 8:56
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date birth;
    private List<Dog> dogs;
    private Map<String, Object> maps;
    private List<Object> lists;

    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 boolean isBoss() {
        return boss;
    }

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

    public Date getBirth() {
        return birth;
    }

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

    public List<Dog> getDogs() {
        return dogs;
    }

    public void setDogs(List<Dog> dogs) {
        this.dogs = dogs;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", dogs=" + dogs +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}

3. 入口文件添加@EnableConfigurationProperties

package cn.jfjb.sbhelloworld01quick;

import cn.jfjb.bean.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({Person.class})
public class SbHelloworld01QuickApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbHelloworld01QuickApplication.class, args);
    }

}

4. 编写application.yaml文件

person:
  last-name: "aa"
  age: 12
  boss: false
  birth: 2019/11/11
  dogs:
    - {name: "aa",age: 11}
    - {name: "bb",age: 12}
    - {name: "cc",age: 14}
  maps:
    aa: 1
    bb: "aas"
  lists:
    - 1
    - 2
    - 3
    - 4
    - 5
    - 6

4. 进行测试

package cn.jfjb.sbhelloworld01quick;

import cn.jfjb.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbHelloworld01QuickApplicationTests {

    @Autowired
    Person person;

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

}

效果如图

posted @ 2019-11-22 09:30  if年少有为  阅读(766)  评论(0编辑  收藏  举报