作为一个Springboot的初学者,在使用Springboot 2.6.7一段时间后对Springboot的启动流程比较好奇,就简单地记录了学习了一下Springboot在启动时做的一些事情。
每个Springboot应用都有一个启动类,启动类中的main方法中执行了SpringApplication的run方法。
@SpringBootApplication
public class SpringtestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringtestApplication.class, args);
}
}
SpringApplication中run方法的返回值是一个ConfigurableApplicationContext对象,通过查看源码得知ConfigurableApplicationContext接口继承了ApplicationContext接口、Lifecycle接口和Closeable接口,表示ConfigurableApplicationContext对象是一个具有Spring生命周期并且可以自动关闭的上下文对象。ConfigurableApplicationContext接口中定义了一些在创建和初始化上下文对象的时候所必须的Bean的名称。
通过看源码run方法其实是做了一下两件事:
1.new一个SpringApplication对象;
2.执行该对象的run方法。
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
在创建SpringApplication对象的时候,主要是做了一下事情:
public SpringApplication(Class<?>... primarySources) {
this((ResourceLoader)null, primarySources);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = Collections.emptySet();
this.isCustomEnvironment = false;
// 设置Bean的加载方式,是否为懒加载
this.lazyInitialization = false;
// 设置上下文工厂的类型
this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
// 设置应用的启动类型
this.applicationStartup = ApplicationStartup.DEFAULT;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
// 获取所有资源Class的set集合
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
// 判断应用的类型
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 获取所有引导器的ArrayList对象
this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
// 获取所有初始化器的ArrayList对象
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 获取所有监听器的ArrayList对象
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
// 获取启动类的Class对象
this.mainApplicationClass = this.deduceMainApplicationClass();
}
执行该对象的run方法主要做了以下事情
public ConfigurableApplicationContext run(String... args) {
// 启动时间
long startTime = System.nanoTime();
// 利用引导器(bootstrapRegistryInitializers)创建一个初始的应用上下文bootstrapContext
DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
ConfigurableApplicationContext context = null;
// 设置系统属性"java.awt.headless"的值,默认值为:true
this.configureHeadlessProperty();
// 根据参数创建所有的spring运行监听器
SpringApplicationRunListeners listeners = this.getRunListeners(args);
// 利用监听器响应spring应用启动事件
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
// 参数封装
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 根据运行监听器和参数来准备 Spring 的运行环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
this.configureIgnoreBeanInfo(environment);
// 创建 Banner 打印类
Banner printedBanner = this.printBanner(environment);
// 根据当前应用的类型(webApplicationType)来创建对应的ApplicationContext对象(IOC容器)
context = this.createApplicationContext();
// 在IOC容器中设置当前应用的启动方式
context.setApplicationStartup(this.applicationStartup);
// 初始化IOC容器,将初始的上下文对象、环境变量、运行监听器、参数加载到IOC容器中
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
// 刷新IOC容器
this.refreshContext(context);
// 刷新IOC容器后需要进行的处理(默认为空方法,可以通过继承SpringApplication类来重写)
this.afterRefresh(context, applicationArguments);
// 计算IOC容器从创建到启动所需的时间
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
// 若启用了日志打印,则会打印出启动类的类名以及启动时间等
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
}
// IOC容器启动完成,监听器执行对应的响应事件(观察者模式)
listeners.started(context, timeTakenToStartup);
// 利用参数加载并运行所有的运行器
this.callRunners(context, applicationArguments);
} catch (Throwable var12) {
this.handleRunFailure(context, var12, listeners);
throw new IllegalStateException(var12);
}
try {
// 计算IOC容器从创建到准备完成所需的时间
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
// IOC容器准备完成,监听器执行对应的响应事件
listeners.ready(context, timeTakenToReady);
return context;
} catch (Throwable var11) {
this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var11);
}
}