【Spring Boot】(6)、Profile

Profile是Spring对不同环境提供不同配置功能的支持,可通过激活、指定参数等方式快速切换环境。

1、多Profile文件(Properties格式)

在主配置文件编写的时候,文件名可以是application-{profile}.properties

  • application.properties: 默认全局配置文件

  • application-dev.properties: 开发环境

  • application-prod.properties: 生产环境

  • application-test.properties: 测试环境


2、yml支持多文档块方式

server:
  port: 8080
spring:
  profiles:
    active: dev

---
server:
  port: 8084
spring:
  profiles: dev


---
server:
  port: 8085
spring:
  profiles: prod


---
server:
  port: 8086
spring:
  profiles: test


3、激活指定Profile

  • 在全局配置文件中指定spring.profiles.active=...来指定当前的环境,并使用对应的application-{profile}.properties或者application.yml所对应profile下的配置作为当前环境使用的配置信息。

  • 虚拟机参数:Edit Configurations ---> VM options ---->-Dspring.profiles.active=...

  • 命令行:

    • 打包用命令java -jar xxxx.jar --spring.profiles.active=...

    • IDEA运行设置:Edit Configurations ---> Program arguments ----> --spring.profiles.active=xx



====================打个广告,欢迎关注====================

QQ:
412425870
微信公众号:Cay课堂

csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)




点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




猜你喜欢

转载自blog.csdn.net/caychen/article/details/79968021