解析SpringBoot 多Profile的使用以及切换方式

猿友 2021-07-23 10:33:45 浏览数 (1688)
反馈

本篇文章将为您介绍关于 SpringBoot 中多 Profile 的使用以及切换方式,以供大家学习参考,希望这篇文章能够帮助到大家的学习。以下是详情内容。

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

文件名格式:application-{profile}.properties
可以建立多个properties(yaml)文件来不断的切换

application-dev.properties

server.port=8082

application-prod.properties

server.port=8083

application.properties

server.port=8081
spring.profiles.active=dev

在这里插入图片描述

文件名格式:application-{profile}.yaml

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

激活方式:

yaml中:
spring:
  profiles:
    active: dev
    或
properties中:
spring.profiles.active=dev

运行时:

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

java -jar spring-boot.jar --spring.profiles.active=dev;

tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件:set JAVA_OPTS="-Dspring.profiles.active=test"

web.xml方式

spring.profiles.active prod
标注方式(junit单元测试非常实用)
@ActiveProfiles({“dev”})

到此这篇关于 SpringBoot 多 Profile 的使用与切换方式的文章就介绍到这了,想要了解更多相关 SpringBoot 框架其他方面应用的内容请搜索W3Cschool以前的文章或继续浏览下面的相关文章,希望大家以后多多支持我们!


0 人点赞