文章目录
源码分析jar为spring Boot 2.3.1
1.Spring Boot启动原理
- 创建SpringApplication
- 保存信息
- 判断应用类型 是webflux还是servlet
- ApplicationContextInitializer:去spring.factories找 ApplicationContextInitializer
- ApplicationListener:应用监听器 去spring.factories找 ApplicationContextInitializer
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
- 运行SpringApplication
- stopWatch
- 记录应用启动时间
- 让当前应用进入headless java.awt.headless
- 获取所有运行监听器getRunListeners(args);
- 获取SpringApplicationRunListener
- 遍历 start
- 这里可以监听系统启动过程 可以自定义类
- 保存命令行参数 applicationArguments
- 准备环境prepareEnvironment
- 得到基础环境信息StandardEnvironment
- 配置基础环境信息
- 读取所有配置文件 configurePropertySources
- 激活环境 configureProfiles
- 监听器调用listener.environmentPrepared();通知所有的监听器当前环境准备完成
- 配置忽略bean的信息
- 创建IOC容器
- 根据类型创建不同的applicationcontext
- 准备ApplicationContext IOC容器的基本信息 prepareContext()
- 保存环境信息
- 后置处理
- 应用初始化:applyInitializers 对应创建SpringApplication创建的ApplicationContextInitializer
- 调用initialize 对ioc容器进行初始化扩展
- 监听器调用listener.contextPrepared()
- 监听器调用listener.contextLoaded()
- refreshContext():spring核心源码
- afterRefresh(context, applicationArguments)
- 也可以扩展自己的实现
- 监听器调用listener.started()
- 调用所有的runners
- 获取容器中的ApplicationRunner
- 获取容器中的CommandLineRunner
- AnnotationAwareOrderComparator 排序合并
- 监听器调用listener.running()
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}