springboot多環(huán)境配置文件及自定義配置文件路徑詳解
一:什么是classpath?
classpath指的就是 *.java文件,資源文件等編譯后存放的位置,對于maven項目就是指 target/classes:這個路徑,只要編譯后的文件在這個目錄下,springboot就可以找到,這里指的是maven項目,javaWeb項目可能會有區(qū)別。
二、自定義springboot配置文件路徑
springboot項目配置文件application.properties或者application.yml配置文件默認放置的位置是 **“classpath:/,classpath:/config/,file:./,file:./config/ ”**這4個位置。只要我們編譯后的文件位于這4個位置,springboot就可以加載配置文件。
但有時候我們需要以環(huán)境名稱為標識,配置多個環(huán)境的配置文件。如下我們需要配置dev1、dev2、stg1、stg2、prd 共5個環(huán)境,每個環(huán)境下分別配置我們的application.properties,數(shù)據(jù)庫配置,redis配置,日志配置等配置。
這時我們的資源文件的路徑已經(jīng)不再是springboot默認的配置路徑了,因此springboot啟動時將無法加載配置文件,這里就需要我們在啟動類中手動指定配置文件的加載路徑了。如下:
public class DemoApplication { public static void main(String[] args) { //springboot默認的配置文件路徑 // 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的加載配置項在ConfigFileApplicationListener類中,可以自行翻看下源碼定義。如下是我們從spring源碼中復(fù)制過來的一分部,可以發(fā)現(xià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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC使用自定義驗證器進行數(shù)據(jù)驗證的方法
SpringMVC?提供了強大的數(shù)據(jù)驗證機制,可以方便地驗證表單提交的數(shù)據(jù),除了自帶的驗證器之外,SpringMVC?還支持自定義驗證器,允許開發(fā)者根據(jù)業(yè)務(wù)需求自定義驗證規(guī)則,本文將介紹如何在?SpringMVC?中使用自定義驗證器2023-07-07SpringBoot Redis批量存取數(shù)據(jù)的操作
這篇文章主要介紹了SpringBoot Redis批量存取數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java 實現(xiàn)word模板轉(zhuǎn)為pdf
這篇文章主要介紹了Java 實現(xiàn)word模板轉(zhuǎn)為pdf的方法,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下2021-02-02springmvc+shiro+maven 實現(xiàn)登錄認證與權(quán)限授權(quán)管理
Shiro 是一個 Apache 下的一開源項目項目,旨在簡化身份驗證和授權(quán),下面通過實例代碼給大家分享springmvc+shiro+maven 實現(xiàn)登錄認證與權(quán)限授權(quán)管理,感興趣的朋友一起看看吧2017-09-09RestTemplate對HttpClient的適配源碼解讀
這篇文章主要為大家介紹了RestTemplate對HttpClient的適配源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10