五、Spring Boot 2 --- YAML 配置文件
除了可以使用 properties 类型的配置文件,还可以使用 yaml 类型的配置文件
5.1 介绍
YAML 有两种解释
- YAML Ain't Markup Language:YAML 不是一种标记语言
- Yet Another Markup Language:仍是一种标记语言
yaml 类型的配置文件由于其层次分明的书写格式,非常适合做以数据为中心的配置文件
springboot 的主配置文件 application.properties
可以使用 application.yaml
代替
5.2 基本语法
- 格式:
key: value
,value
与:
之间有空格 - 大小写敏感
- 缩进代表层级关系
- 同一层级的元素左对齐
- 缩进不允许使用 tab,只允许使用空格(IDEA 中可以使用 tab)
#
代表注释- 字符串无需加引号
- 单引号表示转义
- 双引号表示不转义
5.3 使用
以一个实际场景为例说明 yaml 文件的语法
@ConfigurationProperties(prefix = "person") @Component @Data @ToString public class Person { private String userName; private Boolean boss; private Date birth; private Integer age; private String[] interests; private List<String> animal; private Set<Double> salarys; private Pet pet; private Map<String, Object> score; private Map<String, List<Pet>> allPets; }
@Data @ToString public class Pet { private String name; private Double weight; }
Person 类与 application.yaml 进行了配置绑定,前缀为 person
现在通过 application.yaml 配置文件对注入到 ioc 容器中的 person 组件进行赋值
1、private String userName
对应的配置项: person.userName
写法:
person: userName: zhangsan
注意:
- 基本的数据类型,例如:int、double、boolean、...,以及 String,Date,都是上面这种写法
person:
的:
后面无需加空格- 如果使用单引号,例如
'zhangsan\nlisi'
,会进行转义,输出zhangsan\\nlisi
- 如果使用双引号,例如
"zhangsan\nlisi"
,不会进行转义,输出时会进行换行
2、private Boolean boss
对应的配置项:person.boss
写法:
person: boss: true
3、private Date birth
对应的配置项:person.birth
写法:
person: birth: 1990/10/11
注意:yaml 中默认日期的书写格式是:yyyy/MM/dd
4、private Integer age
对应的配置项:person.age
写法:
person: age: 30
5、private String[] interests
对应的配置项:person.interests
写法一:
person: interests: [吃饭, 睡觉, 敲代码]
写法二:
person: interests: - 吃饭 - 睡觉 - 敲代码
注意:-
与 value
之间的空格不能省略
说明:一组按次序排列的值,例如:数组、list、set、...,都有如上的两种写法
6、private List<String> animal
对应的配置项:person.animal
写法一:
person: animal: [猫咪, 狗狗, 兔兔]
写法二:
person animal: - 猫咪 - 狗狗 - 兔兔
7、private Set<Double> salarys
对应的配置项:person.salarys
写法一:
person: salarys: [11111.11, 22222.22]
写法二:
person: salarys: - 1111.11 - 2222.22
8、private Pet pet
对应的配置项:person.pet
该变量是自定义类型的变量,其内部属性对应的配置项是:person.pet.name
、person.pet.weight
写法一:
person: pet: {name: c1, weight: 11.11}
注意:name:
与 c1
之间的空格不能省略, weight
同理
写法二:
person: pet: name: c1 weight: 11.11
说明:对于键值对集合的类型,例如:类、map、...,都有如上的两种写法
9、private Map<String, Object> score
对应的配置项:person.score
写法一:
person: score: {english: 20, math: 50}
写法二:
person: score: english: 20 math: 50
10、private Map<String, List<Pet>> allPets
对应的配置项:person.allPets
写法:
person: allPets: sick: - { name: c1, weight: 11.11 } - name: c2 weight: 22.22 - name: c3 weight: 33.33 health: [ { name: c4, weight: 44.44 },{ name: c5, weight: 55.55 } ]
补充说明:
- 如果属性是用驼峰命名法,则在 properties 或 yaml 配置文件中使用横线命名法也可以实现配置绑定
- 例如 Person 的属性 userName,在 yaml 和 properties 中对应的配置项可以是
person.userName
,也可以是person.user-name
- 例如 Person 的属性 userName,在 yaml 和 properties 中对应的配置项可以是
- 如果一个 springboot 项目的类路径下既有 application.properties 配置文件,也有 application.yaml 配置文件,两个配置文件都能生效但是如果有相同的配置以 application.properties 的配置为准
5.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>
通过上面的配置就可以实现自定义类绑定的配置提示
但是如果配置类存在级联属性,例如 Person 的 pet 属性是自定义类型的变量,也有自己的属性 name 和 weight,还是无法进行提示
如果希望级联属性也有提示,需要在类路径下创建 META-INF/additional-spring-configuration-metadata.json
文件,并进行如下的配置:
{ "properties": [ { "name": "person.pet.name", "type": "java.lang.String", "sourceType": "com.wndex.boot.bean.Pet" }, { "name": "person.pet.weight", "type": "java.lang.String", "sourceType": "com.wndex.boot.bean.Pet" } ] }
注意:完成上述配置后,需要重新进行编译,才能失效
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?