SpringBoot的配置文件
1. properties
properties这里就不做过多的说明了
2. yaml
2.1 基本语法
- key: value形式 (冒号后面有空格)
- 大小写敏感
- 缩进的时候不要用空格
- 缩进没有严格的要求,同一层级主要对齐就可以
- value为字符串不需要加引号,如果用双引号和不加是一样,如果是单引号内容中的字符会被转义
- 注释为 #
'/n' 表示换行
"/n" 就是/n字符串
2.2 数据类型
# 对象
key: {k1: v1, K2: v2}
# 或
key:
k1: v1
k2: v2
# 数组
key: [v1, v2]
# 或
key:
- v1
- v2
2.3 示例
将下面的对象用yaml表示出来
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
public class Pet {
private String name;
private Double weight;
}
Person:
username: zz
boss: true
birth: 2021/11/29
age: 22
pet:
name: 旺财
weight: 10.0
interests: [吃, 喝]
animal:
- 狗
- 猫
score:
yuwen: 100
shuxue: 120
salarys: [2000, 3000]
allPets:
dogs:
- {name: tom, weight: 100}
cats: [{name: jerry, weight: 180}]
2.4 配置文件编写提示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<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>
</plugin>
</plugins>
</build>