2.7 配置文件占位符
1.随机数
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}
2.占位符获取之前的值,如果没有可以用“:”指定默认值
person.yml
person:
age: ${random.int}
last-name: 张三${random.uuid}
boss: false
birth: 2019/10/02
maps:
ki: v1
k2: v2
list:
- lisi
- wangwu
dog:
name: ${person.hello:hello}dog
age: 2
3.测试
2.8 Profile多环境配置
1.多Profile文件形式
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml
默认使用application.properties的配置
2.多profile文档块模式--yml
server:
port: 8083
spring:
profiles:
active: prod
---
server:
port: 8084
spring:
profiles: dev
---
server:
port: 8085
spring:
profiles: prod
3.激活方式
1.在配置文件中指定
spring.profiles.active=dev
2.命令行:
打包 java -jar springboot-01-helloworld-quick-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
可以在测试过程不用打包,直接配置命令行参数(Program arguments) --spring.profiles.active=dev
3.虚拟机参数(VM options)
-Dspring.profiles.active=dev
2.9 配置文件加载位置
spring boot 启动会扫描以下位置的application.properties或application.yml文件作为Spring boot的默认配置文件
- file:./config/
- file ./
-classpath:/config/
-calsspath:/
以上是按照优先级从高到低的顺寻,高优先级的配置内容会覆盖低优先级的配置内容
所有位置的文件都会被加载,互补配置
我们也可以通过配置spring.config.location来改变默认的配置文件位置(通常情况下,打包后要改某个配置,则只需要把该配置修改并存到本地路径中),利用命令行参数的形式java -jar xxx.jar --spring.config.location=d:/application.properties,这个指定的配置文件和默认加载的配置文件会共同起作用你,形成互补配置