1、文件类型
1.1、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,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- ‘#’表示注释
字符串无需加引号,如果要加,’’与””表示字符串内容 会被 转义/不转义
1.2.3、数据类型
字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
对象:键值对的集合。map、hash、object
行内写法: k: {k1:v1,k2:v2,k3:v3}#或k:k1: v1k2: v2k3: v3
数组:一组按次序排列的值。array、list、queue、set
行内写法: k: {k1:v1,k2:v2,k3:v3}#或k:k1: v1k2: v2k3: v3
1.2.4、示例
```java @Data public class Person {
private String userName; private Boolean boss; private Date birth; private Integer age; private Pet pet; private String[] interests; private List
animal; private Map score; private Set salarys; private Map > allPets; }
@Data public class Pet { private String name; private Double weight; }
```yaml# yaml表示以上对象person:userName: zhangsanboss: falsebirth: 2019/12/12 20:12:33age: 18pet:name: tomcatweight: 23.4interests: [篮球,游泳]animal:- jerry- marioscore:english:first: 30second: 40third: 50math: [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}]
其中字符串的值,可以加单引号‘’或双引号“”,也可以都不加。
其区别在于单引号会将字符串里的 \n 作为字符串输出
双引号会将 \n 作为换行输出,即进行了转义,即按照原有字符串意思
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.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></exclude></excludes></configuration></plugin></plugins></build>
