一般情況下,springboot默認(rèn)會(huì)在resource目錄下生成一個(gè)配置文件(Application.properties或application.yaml),但其實(shí)springboot允許配置多個(gè)配置文件(application.properties或application.yaml),但是這并不意味著這些配置文件一定會(huì)替換默認(rèn)生成的配置文件,它們是互補(bǔ)的存在。如果在某些場(chǎng)景下需要把配置文件單獨(dú)拿出來(lái)并且啟動(dòng)的時(shí)候加載進(jìn)去,那么外部的配置文件將是一個(gè)很好的選擇。
配置文件加載順序
需要注意的是配置文件加載順序加載順序在springboot 2.4.0前后是不一樣的。
- springboot 2.4.0及其之前版本的配置文件加載順序
file:./config/file:./config/*/file:./classpath:config/classpath:復(fù)制代碼
- springboot 2.4.0之后版本的配置文件加載順序
file:./config/*/file:./config/file:./classpath:config/classpath:復(fù)制代碼
區(qū)別在于springboot 2.4.0之后的版本將file:./config/*/的在順序調(diào)整為第一加載順序。
file是指當(dāng)前jar包所在路徑。
classpath是指springboot resource文件夾下路徑。
驗(yàn)證
前期準(zhǔn)備
- 新建一個(gè)springboot項(xiàng)目
啟動(dòng)類如下:
@SpringBootApplicationpublic class MqApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(MqApplication.class, args);ConfigurableEnvironment environment = applicationContext.getEnvironment();String property = environment.getProperty("spring.application.name");System.out.println("current spring.application.name="+property);}}復(fù)制代碼
配置文件:
spring.application.name=classpathserver.port=8080復(fù)制代碼
為了驗(yàn)證 springboot 2.4.0之前和之后的版本加載順序的不一樣,會(huì)使用兩個(gè)版本對(duì)比。 對(duì)比版本:springboot 2.4.3 和 springboot 2.3.5.RELEASE
下面是不同路徑下配置不同端口和應(yīng)用名以便驗(yàn)證。
|
路徑 |
端口號(hào) |
application.name |
|
file:./config/*/ |
8084 |
file:./config/*/ |
|
file:./config/ |
8083 |
file:./config/ |
|
file:./ |
8082 |
file:./ |
|
classpath:config/ |
8081 |
classpath:config/ |
|
classpath: |
8080 |
classpath: |
驗(yàn)證配置文件加載順序
根據(jù)上述表格,將配置文件分別復(fù)制到不同的路徑下創(chuàng)建配置文件并按表格修改spring.application.name和server.port屬性值。
啟動(dòng)項(xiàng)目,下面是兩個(gè)版本的啟動(dòng)信息:

從兩張圖中可以得出結(jié)論:
- springboot 2.4.0前后配置文件加載順序不一樣
- 高優(yōu)先級(jí)的會(huì)覆蓋掉低優(yōu)先級(jí)相同的屬性
驗(yàn)證屬性互補(bǔ)
- 修改配置文件:
- classpath:配置文件
刪除spring.application.name屬性,增加server.error.path屬性
server.port=8080server.error.path=/test復(fù)制代碼
- file:./配置文件
新增server.servlet.context-path屬性
spring.application.name=file:.server.port=8082server.servlet.context-path=file_context復(fù)制代碼
- file:./config/*/配置文件
保持不變
server.port=8084spring.application.name=file:./config/*/復(fù)制代碼
- 修改啟動(dòng)類main方法
在控制臺(tái)打印server.error.path
public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(MqApplication.class, args);ConfigurableEnvironment environment = applicationContext.getEnvironment();String property = environment.getProperty("spring.application.name");System.out.println("current spring.application.name="+property);String errorPath = environment.getProperty("server.error.path");System.out.println("errorPath="+errorPath);}復(fù)制代碼
- 啟動(dòng)項(xiàng)目
從上面截圖中可以發(fā)現(xiàn)三個(gè)配置文件中的所有屬性都被加載出來(lái)了,而且優(yōu)先級(jí)高的配置文件中的屬性會(huì)覆蓋優(yōu)先級(jí)低的配置文件中的屬性。
總結(jié)
springboot中可以配置多個(gè)配置文件,并且這些配置文件是可以共存的。當(dāng)屬性相同時(shí),優(yōu)先級(jí)高的配置文件會(huì)覆蓋優(yōu)先級(jí)低的配置文件中的屬性;當(dāng)屬性不同時(shí),最終的配置會(huì)取各個(gè)配置文件中屬性的并集。
作者:索碼理
鏈接:
https://juejin.cn/post/7126394308294344711
來(lái)源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。






