Spring Boot 启动过程

阅读源码是最快的学习途径

1
2
3
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader; // null
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); // 带有@SpringBoot的主类
this.webApplicationType = WebApplicationType.deduceFromClasspath(); //SERVLET
/**
通过 SpringFactoriesLoader 从默认资源路径 META-INF/spring.factories 加载
com.learn.config.MyBootstrapRegistryInitializer
org.springframework.cloud.bootstrap.RefreshBootstrapRegistryInitializer
org.springframework.cloud.bootstrap.TextEncryptorConfigBootstrapper
*/
this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
/**
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
*/
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
/**
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
*/
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass(); // 找 main 方法
}