前言
用了那么久的spring boot了一直停留在使用层面,最近想提高一下,所以花一点时间来研究一下springboot的源码,这里只是简单的跟一下启动流程,然后这其中提到的那个关键接口和类会单独再做解析
spring boot 启动
springboot 启动代码如下
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication .class, args);
}
}
我们再来接着跟踪代码 这里就是springboot运行的概述先用构造方法初始化一些数据,然后再调用run方法来创建容器和一些容器相关的实体
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
我们再来看构造方法主要做一些容器启动初始化的操作
public SpringApplication(Class... primarySources) {
this((ResourceLoader)null, primarySources);
}
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;//控制banner展示类型
this.logStartupInfo = true; //控制启动日志输出
this.addCommandLineProperties = true; //控制是否添加命令行配置
this.headless = true; //控制项目以headless模式启动(无gui和键盘鼠标)
this.registerShutdownHook = true; //控制是否注册虚拟机关闭监听
this.additionalProfiles = new HashSet();
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));//保存启动的类
this.webApplicationType = this.deduceWebApplicationType();//确定当前运行的web环境
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));//获取ApplicationContextInitializer接口的各个工厂实例并按继承了PriorityOrdered的排前面排序
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));//获取ApplicationListener接口的各个工场实例并按继承了PriorityOrdered的排前面排序
this.mainApplicationClass = this.deduceMainApplicationClass();//记录启动类class用于日志输出
}
构造函数初始化环境之后就开始调用run方法启动容器
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch(); //记录容器启动耗时
stopWatch.start();
ConfigurableApplicationContext context = null;//spring上下文环境
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();//?没看出有什么用
this.configureHeadlessProperty();//设置当前是否为headless启动
SpringApplicationRunListeners listeners = this.getRunListeners(args);//获取SpringApplicationRunListener接口的各个工场实例并按继承了PriorityOrdered的排前面排序
//然后再将各个工厂实例装入SpringApplicationRunListeners类中
listeners.starting();//循环调用刚才初始化的各个SpringApplicationRunListener工厂类的starting方法启动各个SpringApplicationRunListener监听器实例
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//存入启动参数
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);//准备环境
// 结束时会向各个SpringApplicationRunListener发送事件
// environmentPrepared
this.configureIgnoreBeanInfo(environment);//设置是否BeanInfo类 默认为true
Banner printedBanner = this.printBanner(environment); //输出banner信息
context = this.createApplicationContext(); //实例化上下文
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);//获取SpringBootExceptionReporter接口的各个工场实例并按继承了PriorityOrdered的排前面排序 用于在启动错误时回调自定义报告
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);// 准备应用上下文,
// 完成时会向各个SpringApplicationRunListener发送事件
// contextPrepared
this.refreshContext(context);//刷新应用上下文并注册关闭钩子
this.afterRefresh(context, applicationArguments);//空方法
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context); // 应用上下文准备完成
// 向各个SpringApplicationRunListener发送事件
// started
this.callRunners(context, applicationArguments); 调用Spring容器中的ApplicationRunner和CommandLineRunner接口的实现类
} catch (Throwable var10) {
// 应用上下文准备遇到异常时,
// 向各个SpringApplicationRunListener发送事件finished,
// 携带响应异常信息
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
// 应用上下文准备完成
// 向各个SpringApplicationRunListener发送事件
// running
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
参考
https://blog.csdn.net/andy_zhang2007/article/details/79254023
网友评论