欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot詳細(xì)講解多個(gè)配置文件的配置流程

 更新時(shí)間:2022年06月08日 10:40:37   作者:索碼理  
SpringBoot項(xiàng)目是一個(gè)標(biāo)準(zhǔn)的Maven項(xiàng)目,它的配置文件需要放在src/main/resources/下,其文件名必須為application,其存在兩種文件形式,分別是properties和yaml(或者yml)文件

一般情況下,springboot默認(rèn)會在resource目錄下生成一個(gè)配置文件(application.properties或application.yaml),但其實(shí)springboot允許配置多個(gè)配置文件(application.properties或application.yaml),但是這并不意味著這些配置文件一定會替換默認(rèn)生成的配置文件,它們是互補(bǔ)的存在。如果在某些場景下需要把配置文件單獨(dú)拿出來并且啟動的時(shí)候加載進(jìn)去,那么外部的配置文件將是一個(gè)很好的選擇。

配置文件加載順序

需要注意的是配置文件加載順序加載順序在springboot 2.4.0前后是不一樣的。

springboot 2.4.0及其之前版本的配置文件加載順序

file:./config/
file:./config/*/
file:./
classpath:config/
classpath:

springboot 2.4.0之后版本的配置文件加載順序

file:./config/*/
file:./config/
file:./
classpath:config/
classpath:

區(qū)別在于springboot 2.4.0之后的版本將file:./config/*/的在順序調(diào)整為第一加載順序。

file是指當(dāng)前jar包所在路徑。

classpath是指springboot resource文件夾下路徑。

驗(yàn)證

前期準(zhǔn)備

新建一個(gè)springboot項(xiàng)目

啟動類如下:

@SpringBootApplication
public 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);
	}
}

配置文件:

spring.application.name=classpath
server.port=8080

為了驗(yàn)證 springboot 2.4.0之前和之后的版本加載順序的不一樣,會使用兩個(gè)版本對比。

對比版本:springboot 2.4.3 和 springboot 2.3.5.RELEASE

下面是不同路徑下配置不同端口和應(yīng)用名以便驗(yàn)證。

路徑端口號application.name
file:./config/*/8084file:./config/*/
file:./config/8083file:./config/
file:./8082file:./
classpath:config/8081classpath:config/
classpath:8080classpath:

驗(yàn)證配置文件加載順序

根據(jù)上述表格,將配置文件分別復(fù)制到不同的路徑下創(chuàng)建配置文件并按表格修改spring.application.name和server.port屬性值。

啟動項(xiàng)目,下面是兩個(gè)版本的啟動信息:

從兩張圖中可以得出結(jié)論:

  • springboot 2.4.0前后配置文件加載順序不一樣
  • 高優(yōu)先級的會覆蓋掉低優(yōu)先級相同的屬性

驗(yàn)證屬性互補(bǔ)

修改配置文件:

classpath:配置文件

刪除spring.application.name屬性,增加server.error.path屬性

server.port=8080
server.error.path=/test

file:./配置文件

新增server.servlet.context-path屬性

spring.application.name=file:.
server.port=8082
server.servlet.context-path=file_context

file:./config/*/配置文件

保持不變

server.port=8084
spring.application.name=file:./config/*/

修改啟動類main方法在控制臺打印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);
	}

啟動項(xiàng)目

從上面截圖中可以發(fā)現(xiàn)三個(gè)配置文件中的所有屬性都被加載出來了,而且優(yōu)先級高的配置文件中的屬性會覆蓋優(yōu)先級低的配置文件中的屬性。

總結(jié)

springboot中可以配置多個(gè)配置文件,并且這些配置文件是可以共存的。當(dāng)屬性相同時(shí),優(yōu)先級高的配置文件會覆蓋優(yōu)先級低的配置文件中的屬性;當(dāng)屬性不同時(shí),最終的配置會取各個(gè)配置文件中屬性的并集。

到此這篇關(guān)于SpringBoot詳細(xì)講解多個(gè)配置文件的配置流程的文章就介紹到這了,更多相關(guān)SpringBoot配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論