App下載

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

猿友 2021-07-23 10:33:45 瀏覽數(shù) (2142)
反饋

本篇文章將為您介紹關(guān)于 SpringBoot 中多 Profile 的使用以及切換方式,以供大家學(xué)習(xí)參考,希望這篇文章能夠幫助到大家的學(xué)習(xí)。以下是詳情內(nèi)容。

Spring 中 Profile 對(duì)不同環(huán)境提供不同配置功能的支持,可以通過激活、指定參數(shù)等方式快速切換環(huán)境。

文件名格式:application-{profile}.properties
可以建立多個(gè)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  (未指定時(shí)默認(rèn)使用的配置)
server:
  port: 80

激活方式:

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

運(yùn)行時(shí):

在打包后運(yùn)行的時(shí)候,添加參數(shù):

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

tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通過設(shè)置active選擇不同配置文件:set JAVA_OPTS="-Dspring.profiles.active=test"

web.xml方式

spring.profiles.active prod
標(biāo)注方式(junit單元測試非常實(shí)用)
@ActiveProfiles({“dev”})

到此這篇關(guān)于 SpringBoot 多 Profile 的使用與切換方式的文章就介紹到這了,想要了解更多相關(guān) SpringBoot 框架其他方面應(yīng)用的內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持我們!


0 人點(diǎn)贊