在SpringBoot项目的父文件spring-boot-starter-parent
的pom文件中,可以看到
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
SpringBoot支持三种格式的配置文件,且读取配置文件的顺序是yml>yaml>properties,所以,如果有相同的配置信息,最后再加的properties文件内的配置会覆盖之前读取的配置。
1. 配置文件格式写法
以yml为例,看一下常用数据格式的写法:
1. 普通数据
name: zhangsan
age: 18
addr: wuhan
2. 对象数据
- 按一般格式来
person: name: xiaowang age: 18 addr: wuhan
- 行内对象格式
person: {name: zhangsan,age: 18,addr: beijing}
3. 集合、数组数据
- 集合、数组元素是普通字符串
city: - beijing - tianjin - chongqing - shanghai city: [beijing,tianjin,chongqing,shanghai]
- 集合、数组元素是对象
student: - name: tom age: 18 addr: beijing - name: lucy age: 17 addr: tianjin student: [{name: tom,age: 18,addr: beijing},{name: lucy,age: 17,addr: tianjin}]
4. Map的配置
map:
key1: value1
key2: value2
网友评论