Springboot读取YML文件

  • Post author:
  • Post category:其他


现在我们在项目中使用配置文件一般都是properties文件或者yml文件,这里我们对yml的常用语法以及springboot如何读取yml做一个总结,以便后面使用。

一、yml的语法

1、yml的基本规则

大小写敏感

使用缩进表示层级关系

禁止使用tab进行缩进,要使用空格进行缩进

缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级

使用#表示注释

字符串可以不用引号标注

2、数据类型

1、字符串

2、布尔值

3、整数

4、浮点数

5、NULL

6、时间

7、日期

通过例子进行演示:

yml:
  #整数
  age: 23
  #字符串
  name: ssl
  city: 'shanghia\nbeijing'
  address: "shanghai\nbeijing"
  #浮点数
  numFloat: 2.1
  #布尔值
  flag: true

package com.ssl.datasource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "yml")
public class test {
    private int age;
    private String name;
    private String city;
    private String address;
    private float numFloat;
    private boolean flag;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public float getNumFloat() {
        return numFloat;
    }

    public void setNumFloat(float numFloat) {
        this.numFloat = numFloat;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return "test{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", city='" + city + '\'' +
                ", address='" + address + '\'' +
                ", numFloat=" + numFloat +
                ", flag=" + flag +
                '}';
    }
}

输出结果:

test{age=23, name='ssl', city='shanghia\nbeijing', address='shanghai
beijing', numFloat=2.1, flag=true}

3、数据结构

1、Map 散列表

name: John
age: 18
#也可以写在一行
{ name: John, age: 18}

2、List 数组

#YAML
- a
- b
- c
#也可以写在一行
[a, b, c]

3、数据结构的嵌套

3.1、Map嵌套Map

#YAML
websites:
 YAML: yaml.org 
 Ruby: ruby-lang.org 
 Python: python.org 
 Perl: use.perl.org 

3.2 Map嵌套List

#YAML
languages:
 - Ruby
 - Perl
 - Python 
 - c

3.3、List嵌套List

#YAML
-
  - Ruby
  - Perl
  - Python 
- 
  - c
  - c++
  - java
#或者
- [Ruby,Perl,Python]
- [c,c++,java]

3.4、List嵌套Map

#YAML
-
  name: John
  age: 18
- 
  name: Lucy
  age: 16

嵌套代码演示:

yml文件:

person:
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet:
    name: tomcat
    weight: 23.4
  interests: [篮球,游泳]
  animal:
    - jerry
    - mario
  score:
    english:
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  allPets:
    sick:
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]

package com.ssl.datasource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;


@Component
@ConfigurationProperties(prefix = "person")
public class AppConfig {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
//    private String[] animal;
    private List<String> animal;
    private Map<String, Map<String,Integer>> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;


    public static class Pet {
        private String name;
        private float weight;

        public String getName() {
            return name;
        }

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

        public float getWeight() {
            return weight;
        }

        public void setWeight(float weight) {
            this.weight = weight;
        }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Boolean getBoss() {
        return boss;
    }

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

    public Date getBirth() {
        return birth;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public String[] getInterests() {
        return interests;
    }

    public void setInterests(String[] interests) {
        this.interests = interests;
    }

    public List<String> getAnimal() {
        return animal;
    }

    public void setAnimal(List<String> animal) {
        this.animal = animal;
    }

    public Map<String, Map<String, Integer>> getScore() {
        return score;
    }

    public void setScore(Map<String, Map<String, Integer>> score) {
        this.score = score;
    }

    public Set<Double> getSalarys() {
        return salarys;
    }

    public void setSalarys(Set<Double> salarys) {
        this.salarys = salarys;
    }

    public Map<String, List<Pet>> getAllPets() {
        return allPets;
    }

    public void setAllPets(Map<String, List<Pet>> allPets) {
        this.allPets = allPets;
    }

    @Override
    public String toString() {
        return "AppConfig{" +
                "userName='" + userName + '\'' +
                ", boss=" + boss +
                ", birth=" + birth +
                ", age=" + age +
                ", pet=" + pet +
                ", interests=" + Arrays.toString(interests) +
                ", animal=" + animal +
                ", score=" + score +
                ", salarys=" + salarys +
                ", allPets=" + allPets +
                '}';
    }
}



版权声明:本文为sunshunli原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。