Springboot源码之application.yaml读取过程
application.yaml的读取发⽣在SpringApplication#prepareEnvironment()过程中
public ConfigurableApplicationContext 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) {
}
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);
spring怎么读英文怎么读
}
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()) {
ptyList();
}
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.(i)), true));
}
return propertySources;
}
调⽤链:
"main@1" prio=5 tid=0x1 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at org.v.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50)
at org.t.config.StandardConfigDataLoader.load(StandardConfigDataLoader.java:45)
at org.t.config.StandardConfigDataLoader.load(StandardConfigDataLoader.java:34)
at org.t.config.ConfigDataLoaders.load(ConfigDataLoaders.java:102)
at org.t.config.ConfigDataImporter.load(ConfigDataImporter.java:118)
at org.t.solveAndLoad(ConfigDataImporter.java:82)
at org.t.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:118)
at org.t.config.ConfigDataEnvironment.processInitial(ConfigDataEnvironment.java:230)
at org.t.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:217)
at org.t.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:88)
at org.t.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:80)
at org.ApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
at org.ApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
at t.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:203)
at t.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:196)
at t.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:170)  at t.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:148)  at org.t.vironmentPrepared(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.vironmentPrepared(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)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。