SpringBoot中yaml配置

yaml是一种可读性高,用来表示数据序列化的格式。在SpringBoot中也可以使用properties,但是推荐使用yaml。

在SpringBoot中使用一种全局的配置文件,其名称是固定的为application,所以我们的yaml文件就是application.yaml,其语法格式为key:空格value(这里带了个:我觉得就是为了识别key吧)

用yaml配置端口号

yaml基本语法

# 对象
student:
  name: hzy
  age: 20

# 对象行内写法
person: {name: abc, age: 18}

# 数组
pets:
  - cat
  - dog
  - pig
# 数组行内写法
phone: [iphone,huawei,xiaomi]

在SpringBoot中测试

首先写两个实体类

Dog

package com.hzy.pojo;

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

package com.hzy.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,String> map;
    private List<String> list;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, String> map, List<String> list, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.map = map;
        this.list = list;
        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, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public List<String> getList() {
        return list;
    }

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

    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 +
                ", map=" + map +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
}

然后就是我们的配置文件了

application.yaml

person:
  name: hzy
  age: 20
  happy: true
  birth: 2020/11/25
  map: {Java: A, MySQL: B, Linux: C}
  list: [music, code]
  dog:
    name: 旺财
    age: 3

最后就是测试了

package com.hzy;

import com.hzy.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);
    }

}

 Person{name='hzy', age=20, happy=true, birth=Wed Nov 25 00:00:00 CST 2020, map={Java=A, MySQL=B, Linux=C}, list=[music, code], dog=Dog{name='旺财', age=3}}

发布了423 篇原创文章 · 获赞 273 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/104323478