Spring Boot的properties配置文件讀取
我在自己寫點(diǎn)東西玩的時(shí)候需要讀配置文件,又不想引包,于是打算扣點(diǎn)Spring Boot讀取配置文件的代碼出來(lái),當(dāng)然只是讀配置文件沒(méi)必要這么麻煩,不過(guò)反正閑著也是閑著,扣著玩了。
具體啟動(dòng)過(guò)程以前的博客寫過(guò)Spring Boot啟動(dòng)過(guò)程(一),這次入口在SpringApplication類中:
private ConfigurableEnvironment prepareEnvironment( SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) { // Create and configure the environment ConfigurableEnvironment environment = getOrCreateEnvironment(); configureEnvironment(environment, applicationArguments.getSourceArgs()); //此處讀取 listeners.environmentPrepared(environment); if (isWebEnvironment(environment) && this.webApplicationType == WebApplicationType.NONE) { environment = convertToStandardEnvironment(environment); } return environment; }
關(guān)于監(jiān)聽(tīng)器的過(guò)程在開(kāi)頭說(shuō)的那篇的一系列中也說(shuō)的挺細(xì)的,這里不介紹了:
都是監(jiān)聽(tīng)器相關(guān)的部分,略了,SpringApplicationRunListeners類中:
public void environmentPrepared(ConfigurableEnvironment environment) { for (SpringApplicationRunListener listener : this.listeners) { listener.environmentPrepared(environment); } }
EventPublishingRunListener:
onApplicationEnvironmentPreparedEvent事件觸發(fā)org\springframework\boot\spring-boot\2.0.0.BUILD-SNAPSHOT\spring-boot-2.0.0.BUILD-20170421.122111-547-sources.jar!\org\springframework\boot\context\config\ConfigFileApplicationListener.java監(jiān)聽(tīng)器執(zhí)行:
現(xiàn)在這個(gè)postProcessors中包含Json之類其他的監(jiān)聽(tīng)器,不過(guò)我現(xiàn)在只想扣出properties的代碼,別的先略過(guò),反正其實(shí)也沒(méi)什么,本來(lái)也是想看看它的思路,扣著玩,不要太在意。
protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); new Loader(environment, resourceLoader).load(); }
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { this.environment = environment; this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader() : resourceLoader; }
this.classLoader = ClassUtils.getDefaultClassLoader(); //其實(shí)也就是Thread.currentThread().getContextClassLoader();
下面就是真正加載了配置文件的load方法了,先是初始化PropertySourcesLoader和一些臨時(shí)的集合:
this.propertiesLoader = new PropertySourcesLoader(); this.activatedProfiles = false; this.profiles = Collections.asLifoQueue(new LinkedList<Profile>()); this.processedProfiles = new LinkedList<>(); // Pre-existing active profiles set via Environment.setActiveProfiles() // are additional profiles and config files are allowed to add more if // they want to, so don't call addActiveProfiles() here. Set<Profile> initialActiveProfiles = initializeActiveProfiles(); this.profiles.addAll(getUnprocessedActiveProfiles(initialActiveProfiles));
這些集合其實(shí)如果沒(méi)配置Profile基本是沒(méi)用的,這東西現(xiàn)在已經(jīng)很少用到了,這個(gè)環(huán)境當(dāng)然是沒(méi)配的:
主要是下面這部分:
for (String location : getSearchLocations()) { if (!location.endsWith("/")) { // location is a filename already, so don't search for more // filenames load(location, null, profile); } else { for (String name : getSearchNames()) { load(location, name, profile); } } }
就是去指定目錄下去找各種以application為名字的指定類型的配置文件:
我只關(guān)心application.properties,它是上面循環(huán)中的一次,走進(jìn)了doLoadIntoGroup方法的下面那句:
private Map<String, ?> loadProperties(Resource resource) throws IOException { String filename = resource.getFilename(); if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { return (Map) PropertiesLoaderUtils.loadProperties(resource); } return new OriginTrackedPropertiesLoader(resource).load(); }
這個(gè)resource其實(shí)只是封裝了一下InputStream,具體的讀取。。。反正也沒(méi)啥特別的讀法:
讀出的key和value放在Map<String, OriginTrackedValue>:
private void put(Map<String, OriginTrackedValue> result, String key, OriginTrackedValue value) { if (!key.isEmpty()) { result.put(key, value); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)
- SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解
- 詳解Spring加載Properties配置文件的四種方式
- Spring Boot中配置文件application.properties使用
- 在SpringBoot下讀取自定義properties配置文件的方法
- 詳解Spring Boot加載properties和yml配置文件
- SpringBoot獲取yml和properties配置文件的內(nèi)容
- Spring導(dǎo)入properties配置文件代碼示例
相關(guān)文章
listview點(diǎn)擊無(wú)效的處理方法(推薦)
下面小編就為大家?guī)?lái)一篇listview點(diǎn)擊無(wú)效的處理方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05一篇文章帶你復(fù)習(xí)java知識(shí)點(diǎn)
以下簡(jiǎn)單介紹了下我對(duì)于這些java基本知識(shí)點(diǎn)和技術(shù)點(diǎn)的一些看法和心得,這些內(nèi)容都源自于我這些年來(lái)使用java的一些總結(jié),希望能夠給你帶來(lái)幫助2021-06-06java多線程join()方法的作用和實(shí)現(xiàn)原理解析(應(yīng)用場(chǎng)景)
join方法主要是用于將當(dāng)前線程掛起,等待其他線程結(jié)束后在執(zhí)行當(dāng)前線程,本文通過(guò)應(yīng)用場(chǎng)景分析代碼示例講解java多線程join()方法的作用和實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-07-07springboot結(jié)合mybatis操作事務(wù)配置的處理
在操作數(shù)據(jù)庫(kù)的時(shí)候,經(jīng)常會(huì)使用事務(wù)的處理,本文主要介紹了springboot結(jié)合mybatis操作事務(wù)配置的處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07spring Security的自定義用戶認(rèn)證過(guò)程詳解
這篇文章主要介紹了spring Security的自定義用戶認(rèn)證過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Spring MVC學(xué)習(xí)教程之RequestMappingHandlerMapping匹配
這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之RequestMappingHandlerMapping匹配的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11