Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境
1、多Profile文件
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml
application.properties
application-dev.properties
application-prod.properties
默认使用application.properties的配置;
application-dev.properties
server.port=8082
2、yml支持多文档块方式
server:
port: 8081
spring:
profiles:
active: prod #激活prod环境
---
server:
port: 8083
spring:
profiles: dev #指定属于哪个环境
---
server:
port: 8084
spring:
profiles: prod #指定属于哪个环境
3、激活指定profile
1、在application.properties配置文件中指定 spring.profiles.active=dev
2、命令行:
命令行1:–spring.profiles.active=dev;
命令行2:打成jar包后,
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
3、虚拟机参数;
-Dspring.profiles.active=dev