0%

SpringBoot—使用YAML进行配置绑定

YAML文件非常适合用来做以数据为中心的配置文件,会在SpringBoot开发中经常是用。这里记录了YAML的基本语法,以及使用yaml进行配置绑定的方法。

YAML基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • ‘#’表示注释
  • 字符串无需加引号,如果要加,’’与””表示字符串内容 会被 转义/不转义

YAML数据类型写法

字面量:

字面量指:单个的、不可再分的值。如:date、boolean、String、number、null

  • 关于String 中的引号:
    • 通常字符串不用加引号
    • 单引号会将 \n作为字符串输出(取消原有转义符 \)
    • 双引号会将\n 作为换行输出(保持原有转义符号 \)

写法

1
2
3
4
5
k: v
#关于String 的引号,如:
key: "line \n line"
# 单引号会将 \n作为字符串输出 双引号会将\n 作为换行输出
# 双引号可以转义,单引号不会转义

示例
Bean类:

1
2
3
4
5
6
7
@ConfigurationProperties(prefix = "person")
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
}

Yaml配置文件:

1
2
3
4
5
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18

对象:

对象包括:对象和键值对的集合。如:map、hash、set、object

写法

1
2
3
4
5
6
行内写法:  k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3

示例
Bean类:

1
2
3
4
5
6
7
8
9
10
11
12
13
=========Person类==========
@ConfigurationProperties(prefix = "person")
public class Person {
private Pet pet;
private Map<String, Object> score;
}

=========Pet类==========
public class Pet {
private String name;
private Double weight;
}

Yaml配置文件:

1
2
3
4
5
6
7
8
person:
#分行写法
pet:
name: 阿狗
weight: 99.99

#行内写法
score: {english: 98,math: 89}

数组

数组指:一组按次序排列的值。如:array、list、queue

写法

1
2
3
4
5
6
行内写法:  k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3

示例
Bean类:

1
2
3
4
5
6
7
8
9
10
11
12
=========Person类==========
@ConfigurationProperties(prefix = "person")
public class Person {
private String[] interests;
private Map<String, List<Pet>> allPets;

=========Pet类==========
public class Pet {
private String name;
private Double weight;
}

Yaml配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
person:
#行内写法
interests: [篮球,足球]

#分行写法(综合)
allPets:
#数组 分行写法
sick:
#对象 分行写法
- name: 阿猫
weight: 16.66
- name: 阿狗
weight: 19.99
health:
#对象 行内写法
- {name: 阿花,weight: 199.99}
- {name: 阿明,weight: 199.99}

配置绑定

在实际编程时,我们需要将yaml文件和bean类实现配置绑定。绑定方法的实现与 所述方法相同,需要使用@ConfigurationProperties(prefix = "xxx")的注解。
yaml配置文件创建在sources目录下的application.yaml.

配置注解处理器

自定义的类和配置文件绑定一般没有提示。需要在pom.xml引入“SpringBoot配置注解处理器”
参考文档:https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

因为这个配置注解处理器只是为了方便编程,实际业务不需要,所以打包时要给予排除。参考上述文档,排除方式是在spring-boot-maven-plugin中加入排除的配置。
加入后配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

<!-- 就是这一段在排除“配置注解处理器” -->
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
<!-- over -->

</plugin>
</plugins>
</build>
</project>