• Profile 是 Spring 对不同环境提供不同配置功能的支持,可以通过**、指定参数等方式快速切换环境
  • 项目开发时有开发环境、测试环境、部署环境等,可以通过 profile 配置切换

 

1.多 profile 文件形式


格式:application-{profile}.properties/yml,如:application-dev.properties、application-prod.properties、application-deploy.yml等
1)Spring Boot 默认都是从全局配置文件 application.properties 和 application.yml 进入开始读取
2)可以使用约定格式“application-{profile}.properties/yml”写任意多的配置文件
3)然后在全局配置文件 application.properties 和 application.yml **它们即可,使用“spring.profiles.active=xxx”
 

Profiles 多环境配置

 

  2、yml支持多文档块方式     (在application.yml文件中配置)

Profiles 多环境配置

3、**指定profile 


 
 1、在配置文件中指定 spring.profiles.active=dev   (这是在application.properties文件中配置的)
  2、命令行:
  java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
 
 可以直接在测试的时候,配置传入命令行参数
  3、虚拟机参数;
 -Dspring.profiles.active=dev 

操作截图如下:

Profiles 多环境配置

 

Profiles 多环境配置

还可以使用下面的这种方式

Profiles 多环境配置

 

 

相关文章: