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ā)現(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;
。。。。。。。。。(略)
到此這篇關于springboot多環(huán)境配置文件及自定義配置文件路徑的文章就介紹到這了,更多相關springboot多環(huán)境配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)雙鏈表互相交換任意兩個節(jié)點的方法示例
這篇文章主要介紹了Java實現(xiàn)雙鏈表互相交換任意兩個節(jié)點的方法,簡單講述了雙鏈表的概念,并結合實例形式給出了java雙鏈表實現(xiàn)任意兩個節(jié)點交換的操作技巧,需要的朋友可以參考下2017-11-11
SpringBoot實現(xiàn)阿里云快遞物流查詢的示例代碼
本文將基于springboot實現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2021-10-10
javax.validation包里@NotNull等注解的使用方式
這篇文章主要介紹了javax.validation包里@NotNull等注解的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

