Spring Boot的properties配置文件讀取
我在自己寫點東西玩的時候需要讀配置文件,又不想引包,于是打算扣點Spring Boot讀取配置文件的代碼出來,當然只是讀配置文件沒必要這么麻煩,不過反正閑著也是閑著,扣著玩了。
具體啟動過程以前的博客寫過Spring Boot啟動過程(一),這次入口在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; }
關于監(jiān)聽器的過程在開頭說的那篇的一系列中也說的挺細的,這里不介紹了:
都是監(jiā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)聽器執(zhí)行:
現在這個postProcessors中包含Json之類其他的監(jiān)聽器,不過我現在只想扣出properties的代碼,別的先略過,反正其實也沒什么,本來也是想看看它的思路,扣著玩,不要太在意。
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(); //其實也就是Thread.currentThread().getContextClassLoader();
下面就是真正加載了配置文件的load方法了,先是初始化PropertySourcesLoader和一些臨時的集合:
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));
這些集合其實如果沒配置Profile基本是沒用的,這東西現在已經很少用到了,這個環(huán)境當然是沒配的:
主要是下面這部分:
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為名字的指定類型的配置文件:
我只關心application.properties,它是上面循環(huá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(); }
這個resource其實只是封裝了一下InputStream,具體的讀取。。。反正也沒啥特別的讀法:
讀出的key和value放在Map<String, OriginTrackedValue>:
private void put(Map<String, OriginTrackedValue> result, String key, OriginTrackedValue value) { if (!key.isEmpty()) { result.put(key, value); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java多線程join()方法的作用和實現原理解析(應用場景)
join方法主要是用于將當前線程掛起,等待其他線程結束后在執(zhí)行當前線程,本文通過應用場景分析代碼示例講解java多線程join()方法的作用和實現原理,感興趣的朋友一起看看吧2021-07-07Spring MVC學習教程之RequestMappingHandlerMapping匹配
這篇文章主要給大家介紹了關于Spring MVC學習教程之RequestMappingHandlerMapping匹配的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-11-11