SpringBoot中yml多環(huán)境配置的3種方法
方式一:多個(gè)yml文件
步驟一、創(chuàng)建多個(gè)配置文件
application.yml #主配置文件 application-dev.yml #開發(fā)環(huán)境的配置 application-prod.yml #生產(chǎn)環(huán)境的配置 application-test.yml #測試環(huán)境的配置
步驟二、applicaiton.yml中指定配置
在application.yml中選擇需要使用的配置文件(當(dāng)選擇的文件和application.yml文件存在相同的配置時(shí),application.yml中的配置會(huì)被覆蓋掉)
spring: profiles: active: dev #需要使用的配置文件的后綴
方式二: 單個(gè)yml文件
#激活dev環(huán)境配置
spring:
profiles.active: dev
# 開發(fā)環(huán)境配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://127.0.0.1:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8080
# 測試環(huán)境配置
spring:
profiles: test
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: test
driver-class-name: com.mysql.jdbc.Driver
server:
port: 88
# 生產(chǎn)環(huán)境配置
spring:
profiles: prod
datasource:
url: jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: prod
driver-class-name: com.mysql.jdbc.Driver
server:
port: 99
配置默認(rèn)的profile為dev,其他環(huán)境可以通過指定啟動(dòng)參數(shù)來使用不同的profile,比如:
測試環(huán)境:java -jar 項(xiàng)目.jar --spring.profiles.active=test
生產(chǎn)環(huán)境:java -jar 項(xiàng)目.jar --spring.profiles.active=prod
方式三:在pom.xml中指定環(huán)境配置
步驟一、創(chuàng)建多個(gè)配置文件
application.yml #主配置文件 application-dev.yml #開發(fā)環(huán)境的配置 application-prod.yml #生產(chǎn)環(huán)境的配置 application-test.yml #測試環(huán)境的配置
步驟二、在application.yml中添加多環(huán)境配置屬性
#多環(huán)境配置
profiles:
active: @profiles.active@
步驟三、在pom.xml中指定使用的配置
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- 默認(rèn)激活-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>
true配置為true則激活對應(yīng)profile的配置。
或如圖所示,在maven->profiles下勾選動(dòng)態(tài)激活需要使用的配置

避坑:不能識(shí)別符號(hào)@
在步驟二中配置的@profiles.active@,啟動(dòng)會(huì)報(bào)異常,不能識(shí)別@符號(hào)。解決方法:
在pom.xml中設(shè)置filtering為true
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
總結(jié):
三種方式都可以實(shí)現(xiàn)多環(huán)境的配置。在application.yml主配置文件中做項(xiàng)目通用的配置,在其他配置文件中做不同環(huán)境下的配置,以避免重復(fù)配置的情況。
以上就是SpringBoot中yml多環(huán)境配置的3種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot yml多環(huán)境配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java 中DH的方式實(shí)現(xiàn)非對稱加密的實(shí)例
這篇文章主要介紹了java 中DH的方式實(shí)現(xiàn)非對稱加密的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)簡單實(shí)例,需要的朋友可以參考下2017-08-08
如何在Java Deeplearning4j中進(jìn)行數(shù)據(jù)加載與預(yù)處理
本文介紹了如何在 Deeplearning4j 中加載和預(yù)處理數(shù)據(jù),包括圖像和 CSV 數(shù)據(jù)的加載,以及基本的數(shù)據(jù)標(biāo)準(zhǔn)化和分割操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-09-09
淺析Spring IOC bean為什么默認(rèn)是單例
單例的意思就是說在 Spring IoC 容器中只會(huì)存在一個(gè) bean 的實(shí)例,無論一次調(diào)用還是多次調(diào)用,始終指向的都是同一個(gè) bean 對象,本文小編將和大家一起分析Spring IOC bean為什么默認(rèn)是單例,需要的朋友可以參考下2023-12-12
Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡單的一對一聊天
這篇文章主要介紹了Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡單的一對一聊天,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
解決spring?security?loginProcessingUrl無效問題
這篇文章主要介紹了解決spring?security?loginProcessingUrl無效問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring?Boot深入學(xué)習(xí)數(shù)據(jù)訪問之Spring?Data?JPA與Hibernate的應(yīng)用
Spring?Data?JPA是Spring?Data的子項(xiàng)目,在使用Spring?Data?JPA之前,先了解一下Hibernate,因?yàn)镾pring?Data?JPA是由Hibernate默認(rèn)實(shí)現(xiàn)的2022-10-10
SpringBoot+Vue實(shí)現(xiàn)數(shù)據(jù)添加功能
這篇文章主要介紹了SpringBoot+Vue實(shí)現(xiàn)數(shù)據(jù)添加功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

