我在自己寫點東西玩的時候需要讀配置文件,又不想引包,於是打算扣點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; }
關於監聽器的過程在開頭說的那篇的一系列中也說的挺細的,這里不介紹了:
都是監聽器相關的部分,略了,SpringApplicationRunListeners類中:
public void environmentPrepared(ConfigurableEnvironment environment) { for (SpringApplicationRunListener listener : this.listeners) { listener.environmentPrepared(environment); } }
EventPublishingRunListener:
onApplicationEnvironmentPreparedEvent事件觸發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監聽器執行:
現在這個postProcessors中包含Json之類其他的監聽器,不過我現在只想扣出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基本是沒用的,這東西現在已經很少用到了,這個環境當然是沒配的:
主要是下面這部分:
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,它是上面循環中的一次,走進了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); } }
以上。
==========================================================
咱最近用的github:https://github.com/saaavsaaa
微信公眾號: