SpringBoot加載外部配置文件


除了默認配置文件,Spring Boot還可以加載一些位於項目外部的配置文件。我們可以通過如下 2 個參數,指定外部配置文件的路徑:

  • spring.config.location 
  • spring.config.additional-location 

spring.config.location

我們可以先將 Spring Boot 項目打包成 JAR 文件,然后在命令行啟動命令中,使用命令行參數 --spring.config.location,指定外部配置文件的路徑。

java -jar {JAR}  --spring.config.location={外部配置文件全路徑}

需要注意的是,使用該參數指定配置文件后,會使項目默認配置文件(application.properties 或 application.yml )失效,Spring Boot 將只加載指定的外部配置文件。

示例:

java -jar springbootdemo-0.0.1-SNAPSHOT.jar  --spring.config.location=D:\myConfig\my-application.yml

spring.config.additional-location

我們還可以在 Spring Boot 啟動時,使用命令行參數 --spring.config.additional-location 來加載外部配置文件。

java -jar {JAR}  --spring.config.additional-location={外部配置文件全路徑}

但與 --spring.config.location 不同,--spring.config.additional-location 不會使項目默認的配置文件失效,使用該命令行參數添加的外部配置文件會與項目默認的配置文件共同生效,形成互補配置,且其優先級是最高的,比所有默認配置文件的優先級都高。

示例:

java -jar springbootdemo-0.0.1-SNAPSHOT.jar  --spring.config.additional-location=D:\myConfig\my-application.yml

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM