springboot多環(huán)境配置文件及自定義配置文件路徑詳解
一:什么是classpath?
classpath指的就是 *.java文件,資源文件等編譯后存放的位置,對(duì)于maven項(xiàng)目就是指 target/classes:這個(gè)路徑,只要編譯后的文件在這個(gè)目錄下,springboot就可以找到,這里指的是maven項(xiàng)目,javaWeb項(xiàng)目可能會(huì)有區(qū)別。
二、自定義springboot配置文件路徑
springboot項(xiàng)目配置文件application.properties或者application.yml配置文件默認(rèn)放置的位置是 **“classpath:/,classpath:/config/,file:./,file:./config/ ”**這4個(gè)位置。只要我們編譯后的文件位于這4個(gè)位置,springboot就可以加載配置文件。
但有時(shí)候我們需要以環(huán)境名稱為標(biāo)識(shí),配置多個(gè)環(huán)境的配置文件。如下我們需要配置dev1、dev2、stg1、stg2、prd 共5個(gè)環(huán)境,每個(gè)環(huán)境下分別配置我們的application.properties,數(shù)據(jù)庫(kù)配置,redis配置,日志配置等配置。
這時(shí)我們的資源文件的路徑已經(jīng)不再是springboot默認(rèn)的配置路徑了,因此springboot啟動(dòng)時(shí)將無(wú)法加載配置文件,這里就需要我們?cè)趩?dòng)類(lèi)中手動(dòng)指定配置文件的加載路徑了。如下:
public class DemoApplication { public static void main(String[] args) { //springboot默認(rèn)的配置文件路徑 // String addClassPath = "spring.config.location:classpath:/"; //自定義的配置文件路徑 String addClassPath = "spring.config.additional-location:classpath:/"; addClassPath += ",classpath:/config/"; addClassPath += ",classpath:/config/dev1/"; addClassPath += ",classpath:/config/dev2/"; addClassPath += ",classpath:/config/stg1/"; addClassPath += ",classpath:/config/stg2/"; addClassPath += ",classpath:/config/prd/"; new SpringApplicationBuilder(DemoApplication.class).properties("spring.config.name:application", addClassPath).build().run(args); }
springboot的加載配置項(xiàng)在ConfigFileApplicationListener類(lèi)中,可以自行翻看下源碼定義。如下是我們從spring源碼中復(fù)制過(guò)來(lái)的一分部,可以發(fā)現(xiàn)一些我們熟悉默認(rèn)配置定義,如spring.profiles.active、classpath:/,classpath:/config/,file:./,file:./config/、spring.config.name等。
public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered { private static final String DEFAULT_PROPERTIES = "defaultProperties"; // Note the order is from least to most specific (last one wins) private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/"; private static final String DEFAULT_NAMES = "application"; private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null); /** * The "active profiles" property name. */ public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; /** * The "includes profiles" property name. */ public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include"; /** * The "config name" property name. */ public static final String CONFIG_NAME_PROPERTY = "spring.config.name"; /** * The "config location" property name. */ public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location"; /** * The "config additional location" property name. */ public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location"; /** * The default order for the processor. */ public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; /** * Name of the application configuration {@link PropertySource}. */ @Deprecated public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties"; private final DeferredLog logger = new DeferredLog(); private String searchLocations; private String names; private int order = DEFAULT_ORDER; 。。。。。。。。。(略)
到此這篇關(guān)于springboot多環(huán)境配置文件及自定義配置文件路徑的文章就介紹到這了,更多相關(guān)springboot多環(huán)境配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC使用自定義驗(yàn)證器進(jìn)行數(shù)據(jù)驗(yàn)證的方法
SpringMVC?提供了強(qiáng)大的數(shù)據(jù)驗(yàn)證機(jī)制,可以方便地驗(yàn)證表單提交的數(shù)據(jù),除了自帶的驗(yàn)證器之外,SpringMVC?還支持自定義驗(yàn)證器,允許開(kāi)發(fā)者根據(jù)業(yè)務(wù)需求自定義驗(yàn)證規(guī)則,本文將介紹如何在?SpringMVC?中使用自定義驗(yàn)證器2023-07-07SpringBoot Redis批量存取數(shù)據(jù)的操作
這篇文章主要介紹了SpringBoot Redis批量存取數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java 實(shí)現(xiàn)word模板轉(zhuǎn)為pdf
這篇文章主要介紹了Java 實(shí)現(xiàn)word模板轉(zhuǎn)為pdf的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-02-02大數(shù)組元素差異removeAll與Map效率對(duì)比
這篇文章主要介紹了大數(shù)組元素差異removeAll與Map效率對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03使用maven?shade插件解決項(xiàng)目版本沖突詳解
這篇文章主要為大家介紹了使用maven?shade插件解決項(xiàng)目版本沖突詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理
Shiro 是一個(gè) Apache 下的一開(kāi)源項(xiàng)目項(xiàng)目,旨在簡(jiǎn)化身份驗(yàn)證和授權(quán),下面通過(guò)實(shí)例代碼給大家分享springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理,感興趣的朋友一起看看吧2017-09-09RestTemplate對(duì)HttpClient的適配源碼解讀
這篇文章主要為大家介紹了RestTemplate對(duì)HttpClient的適配源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10詳解mybatis如何實(shí)現(xiàn)進(jìn)行分表
在數(shù)據(jù)庫(kù)設(shè)計(jì)中,分表是一種常見(jiàn)的優(yōu)化策略,它可以將一個(gè)大表拆分成多個(gè)小表,以提高查詢性能和存儲(chǔ)效率,下面我們就來(lái)學(xué)習(xí)一下mybatis如何實(shí)現(xiàn)進(jìn)行分表吧2023-11-11Java Stream 流實(shí)現(xiàn)合并操作示例
這篇文章主要介紹了Java Stream 流實(shí)現(xiàn)合并操作,結(jié)合實(shí)例形式詳細(xì)分析了Java Stream 流實(shí)現(xiàn)合并操作原理與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05