SpringBoot - 多Profile使用与切换

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

【1】多Profile文件

文件名格式:

application-{profile}.properties

默认使用application.properties配置文件。

如下所示,分别创建application-dev.properties和application-prod.properties文件。

  • application-dev.properties
server.port=8082
  • application-prod.properties
server.port=8083
  • application.properties
server.port=8081
spring.profiles.active=dev

在application.properties中激活了application-dev.properties配置文件。

这里写图片描述


【2】yml多文档快

yml文件中支持使用三个短横线分割文档块的方式。

server:
  port: 8082
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
server:
  port: 8083
---
spring:
  profiles: prod
server:
  port: 8084
---
spring:
  profiles: default
server:
  port: 80
---

其中default表示未指定时默认使用的配置。

这里写图片描述


【3】激活指定配置方式

① 配置文件方式

spring:
  profiles:
    active: dev

spring.profiles.active=dev

② 命令行方式

在打包后运行的时候,添加参数:

java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar 
--spring.profiles.active=dev;

③ 编辑Configurations,填写命令行参数或虚拟机参数

这里写图片描述

猜你喜欢

转载自blog.csdn.net/j080624/article/details/80507927