Spring Boot 配置文件

文件类型

properties

和以前 application.properties 是一样的写法

yaml

简介

YAML 是 “YAML Ain`t Markup Language”(YAML 不是一种标记语言)
在开发时 它仍然是一种标记语言
简介里的这句话,表达的意思是YAML注重的应是数据本身,而不是以标记语言为重点

基本语法

  • key : value ( 特别注意的是 key 和 value之间要有空格分隔 )
  • 大小写敏感
  • 使用缩进来表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • 和 properties文件一样 也是用 ‘#’ 表示注释的
  • ‘ ‘与”” 表示字符串内容 会被 转义/不转移

数据类型

  • 字面量:单个的、不可再分的值。date、boolean、string、number、null

    1
    k: v
  • 对象:键值对的集合。map、hash、set、object

    1
    2
    3
    4
    5
    6
    行内写法:  k: {k1:v1,k2:v2,k3:v3}
    #或
    k:
    k1: v1
    k2: v2
    k3: v3
  • 数组:一组按次序排列的值。array、liet、queue

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

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Data
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;
}

@Data
public class Pet {
private String name;
private Double weight;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# yaml表示以上对象
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}]

配置提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       <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>