SpringBoot配置文件-yaml的使用
-
文件类型
-
properties
和以前的properties的用法一样
-
yaml
-
简介
YAML时"YAML Ain’t Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML的意思其实是:“yet Another Markup Language”(仍是一种标记语言)
非常适合用来做以数据为中心的配置文件
-
基本语法
- key : value ;kv之间有空格(key 冒号 空格 value)
- 大小写敏感
- 使用锁紧表示层级关系
- 锁紧不允许使用tab,只允许空格
- 锁紧的空格数不重要,只要相同层级的元素左对齐即可
- #表示注释
- “与”表示字符串内容会被 转义/不转义
-
数据类型
-
字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
-
对象:键值对的集合。map、hash、set、object
行内写法: k: {k1:v1,k2:v2,k3:v3} #或 k: k1:v1 k2:v2 k3:v3
-
数组:一组按次序排列的值。array、list、queue
行内写法: k: {k1:v1,k2:v2,k3:v3} #或 k: - v1 - v2 - v3
-
示例
@Data @ToString 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> salary; private Map<String,List<Pet>> allPet; } @Data @ToString public class Pet{ private String name; private Double weight; }
yaml表示上述的对象
person: userName: zhangsan boss: true birth: 2020/12/12 age: 28 # interests: [篮球,足球] interests: - 篮球 - 足球 - 排球 animal: [阿猫,阿狗] # score: # english: 90 # math: 90 score: {english:80,math:90} salary: - 9999.98 - 9999.99 pet: name: 阿狗 weight: 10.01 allPet: sick: - {name: 阿狗,weight: 10.01} - name: 阿毛 weight: 9.99 - name: 阿虫 weight: 1.99 health: - {name: 阿花,weight: 10.10} - {name: 阿牛,weight: 99.99}
-
-
-
**添加配置提示依赖**
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
```
**添加的配置提示依赖只是便于我们的开发过程,对开发结果并没有意义,所以在打包的时候建议引入插件取出配置提示**
```xml
<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>
```