阅读源码是最快的学习途径
1 | public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { |
1 | public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { |
- BootstrapRegistryInitializer
- ApplicationContextInitializer
- org.springframework.boot.context.config.DelegatingApplicationContextInitializer@550ee7e5
- org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer@5f9b2141
- org.springframework.boot.context.ContextIdApplicationContextInitializer@247d8ae
- com.learn.config.MyApplicationContextInitializer@48974e45
- org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer@6a84a97d
- org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer@6c130c45
- org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer@50ad3bc1
- org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener@223aa2f7
- ApplicationListener
- org.springframework.cloud.bootstrap.BootstrapApplicationListener@7c711375
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener@57cf54e1
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener@5b03b9fe
org.springframework.boot.context.config.AnsiOutputApplicationListener@37d4349f
org.springframework.boot.context.logging.LoggingApplicationListener@434a63ab
org.springframework.boot.autoconfigure.BackgroundPreinitializer@6e0f5f7f
org.springframework.boot.context.config.DelegatingApplicationListener@2805d709
org.springframework.cloud.context.restart.RestartListener@3ee37e5a
org.springframework.boot.builder.ParentContextCloserApplicationListener@2ea41516
com.learn.config.MyApplicationListener@3a44431a
org.springframework.boot.ClearCachesApplicationListener@3c7f66c4
org.springframework.boot.context.FileEncodingApplicationListener@194bcebf
public class BootstrapApplicationListener implements ApplicationListener, Ordered {}
监听 EnvironmentPreparedEvent,从 bootstrap 加载配置,取名为 springCloudDefaultProperties,高优先级1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
// "spring.cloud.bootstrap.enabled=false" && isNotPresent(org.springframework.cloud.bootstrap.marker.Marker)
// && "spring.config.use-legacy-processing=false"
if (!bootstrapEnabled(environment) && !useLegacyProcessing(environment)) {
return;
}
// don't listen to events in a bootstrap context 跳过 BootstrapContext,即只处理 ApplicationContext
// properties.contains("bootstrap")
if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
return;
}
ConfigurableApplicationContext context = null;
// 找出 bootstrap 配置
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
for (ApplicationContextInitializer<?> initializer : event.getSpringApplication().getInitializers()) {
if (initializer instanceof ParentContextApplicationContextInitializer) {
context = findBootstrapContext((ParentContextApplicationContextInitializer) initializer, configName);
}
}
if (context == null) {
context = bootstrapServiceContext(environment, event.getSpringApplication(), configName);
event.getSpringApplication().addListeners(new CloseContextOnFailureApplicationListener(context));
}
apply(context, event.getSpringApplication(), environment);
} - org.springframework.cloud.bootstrap.LoggingSystemShutdownListener@57cf54e
- org.springframework.boot.env.EnvironmentPostProcessorApplicationListener@
- org.springframework.boot.context.config.AnsiOutputApplicationListener@37d
- org.springframework.boot.context.logging.LoggingApplicationListener@434a6
- org.springframework.boot.autoconfigure.BackgroundPreinitializer@6e0f5f7f
- org.springframework.boot.context.config.DelegatingApplicationListener@280
- org.springframework.cloud.context.restart.RestartListener@3ee37e5a
- org.springframework.boot.builder.ParentContextCloserApplicationListener@2
- com.learn.config.MyApplicationListener@3a44431a
- org.springframework.boot.ClearCachesApplicationListener@3c7f66c4
- org.springframework.boot.context.FileEncodingApplicationListener@194bcebf
- org.springframework.cloud.bootstrap.BootstrapApplicationListener@7c711375