SpringBoot 如何根據(jù)不同profile選擇不同配置
SpringBoot 根據(jù)不同profile選擇不同配置



附上pom的 profiles配置
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profilesActive>dev</profilesActive>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profilesActive>test</profilesActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profilesActive>prod</profilesActive>
</properties>
<build>
<filters>
<filter>/Users/xxx/Desktop/prod.properties</filter>
</filters>
</build>
</profile>
</profiles>
如果使用的是 application-prod.yml 文件
里面的占位符會(huì)被prod.properties替換掉
占位符用 兩個(gè)@@ 例如:@jdbc.name@
SpringBoot不同profile過(guò)濾配置文件
項(xiàng)目多數(shù)情況下,需要部署到不同環(huán)境中.使用maven打包,不做其它配置時(shí),默認(rèn)輸出的jar包中,包含所有的配置文件,這樣顯然不太合理,當(dāng)不同的生產(chǎn)環(huán)境中,不希望存在其它的生產(chǎn)環(huán)境的配置文件,可采用如下配置
1.pom文件中追加profile配置
<!--不同開(kāi)發(fā)環(huán)境配置-->
<profiles>
<profile>
<id>dev</id>
<properties>
<!--pa為自定義的屬性,可在application.yml文件中引用-->
<pa>dev</pa>
</properties>
<activation>
<!--不加參數(shù)默認(rèn)的profile-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<pa>test</pa>
</properties>
</profile>
<profiles>
<!--節(jié)點(diǎn)層級(jí)為project>profiles-->
2.pom文件中build節(jié)點(diǎn)下追加資源過(guò)濾選項(xiàng)
<resources>
<!--先忽略所有配置文件,后進(jìn)行需要的配置文件的追加-->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/application-*.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<!--此處的pa代表的是profile中自定義的屬性-->
<include>**/application-${pa}.yml</include>
</includes>
</resource>
</resources>
3.application.yml文件中引用profile中定義參數(shù)
spring:
profiles:
# profile中定義的pa屬性
active: @pa@
4.手動(dòng)打包指定環(huán)境
mvn clean package -Dmaven.test.skip=true -Pdev
- -Dmaven.test.skip=true 表示跳過(guò)測(cè)試,可提高打包速度
- -P后面的參數(shù)為profile的名稱
5.開(kāi)發(fā)狀態(tài)下切換不同環(huán)境

- 1.IDEA側(cè)邊欄Maven->Profiles勾選需要的環(huán)境
- 2.項(xiàng)目目錄下,終端中執(zhí)行命令mvn -DskipTests=true clean install
- 3.首次切換需要進(jìn)行如下操作,后續(xù)調(diào)試運(yùn)行無(wú)需更改
PS
如上配置后,直接更改spring.profiles.active無(wú)效,且項(xiàng)目運(yùn)行會(huì)報(bào)錯(cuò)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaSE static final及abstract修飾符實(shí)例解析
這篇文章主要介紹了JavaSE static final及abstract修飾符實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作
這篇文章主要介紹了java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法
這篇文章主要介紹了MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解
這篇文章主要介紹了IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12
Java實(shí)現(xiàn)簡(jiǎn)單銀行ATM功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)銀行ATM簡(jiǎn)單功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
使用cmd根據(jù)WSDL網(wǎng)址生成java客戶端代碼的實(shí)現(xiàn)
這篇文章主要介紹了使用cmd根據(jù)WSDL網(wǎng)址生成java客戶端代碼的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03

