1、文件类型

1.1、properties

同以前的properties用法

1.2、yaml

1.2.1、简介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。

非常适合用来做以数据为中心的配置文件

1.2.2、基本语法

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

1.2.3、数据类型

  • 字面量:单个的、不可再分的值。date、boolean、string、number、null
k:
  • 对象,Map集合:键值对的形式。
行内写法:  k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3:
  • 数组:一组按次序排列的值。array、list、queue
行内写法:  k: [v1,v2,v3]
#或者
k:
- v1
- v2
-

1.2.4、示例

@ConfigurationProperties(prefix = "person")
@Component
@ToString
@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;
}
# 注意:单引号会将\n作为字符串输出,而双引号 会将\n作为换行输出
# 双引号不会进行转义,单引号会进行转义.
person:
userName: 'zhangsan \n 李四'
boss: false
birth: 2021/9/6
age: 21
# Pet: {name:tomcat,weight:99.99}
pet:
name: tomcat
weight: 99.99
# interests: [篮球,足球]
interests:
- 篮球
- 足球
- 21
animal: [阿猫,阿狗]
score: {english: 80,math: 90}
salarys:
- 9999.99
- 9999.85
allPets:
sick:
- {name: 阿狗,weight: 99.99}
- name: 阿猫
weight: 88.88
- name: 阿虫
weight: 77.77
health:
- {name: 阿狗,weight: 99.99}
- {name: 阿猪,weight: 99.99}

2、配置提示

自定义的类和配置文件绑定一般没有提示,我们可以这样配置。

<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>