Springboot源碼之application.yaml讀取過程


application.yaml的讀取發生在SpringApplication#prepareEnvironment()過程中

public ConfigurableApplicationContext run(String... args) {
    try {
        ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        ......
    }
}

這個過程會觸發一些監聽器,去執行邏輯。其中在SimpleApplicationEventMulticaster廣播器遍歷監聽器,遍歷到EnvironmentPostProcessorApplicationListener,在這個類入口去處理資源文件。

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
	ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
	Executor executor = getTaskExecutor();
	for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
		if (executor != null) {
			executor.execute(() -> invokeListener(listener, event));
		}
		else if (this.applicationStartup != null) {
			StartupStep invocationStep = this.applicationStartup.start("spring.event.invoke-listener");
			invokeListener(listener, event);
			invocationStep.tag("event", event::toString);
			if (eventType != null) {
				invocationStep.tag("eventType", eventType::toString);
			}
			invocationStep.tag("listener", listener::toString);
			invocationStep.end();
		}
		else {
		    // 反射調用具體監聽器onApplicationEvent方法
			invokeListener(listener, event);
		}
	}
}

其后可根據下面提供的調用鏈去debug

最后yaml、yml文件是用YamlPropertySourceLoader加載器去加載

@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
	if (!ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
		throw new IllegalStateException(
				"Attempted to load " + name + " but snakeyaml was not found on the classpath");
	}
	List<Map<String, Object>> loaded = new OriginTrackedYamlLoader(resource).load();
	if (loaded.isEmpty()) {
		return Collections.emptyList();
	}
	List<PropertySource<?>> propertySources = new ArrayList<>(loaded.size());
	for (int i = 0; i < loaded.size(); i++) {
		String documentNumber = (loaded.size() != 1) ? " (document #" + i + ")" : "";
		propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber,
				Collections.unmodifiableMap(loaded.get(i)), true));
	}
	return propertySources;
}

調用鏈:

"main@1" prio=5 tid=0x1 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50)
	  at org.springframework.boot.context.config.StandardConfigDataLoader.load(StandardConfigDataLoader.java:45)
	  at org.springframework.boot.context.config.StandardConfigDataLoader.load(StandardConfigDataLoader.java:34)
	  at org.springframework.boot.context.config.ConfigDataLoaders.load(ConfigDataLoaders.java:102)
	  at org.springframework.boot.context.config.ConfigDataImporter.load(ConfigDataImporter.java:118)
	  at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:82)
	  at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:118)
	  at org.springframework.boot.context.config.ConfigDataEnvironment.processInitial(ConfigDataEnvironment.java:230)
	  at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:217)
	  at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:88)
	  at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:80)
	  at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
	  at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
	  at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:203)
	  at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:196)
	  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:170)
	  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:148)
	  at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
	  at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
	  at org.springframework.boot.SpringApplicationRunListeners$$Lambda$38.817686795.accept(Unknown Source:-1)
	  at java.util.ArrayList.forEach(ArrayList.java:1257)
	  at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
	  at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
	  at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
	  at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
	  at com.java.study.StudyApplication.main(StudyApplication.java:13)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM