springboot源码分析系列文章
springboot启动流程源码之一(new SpringApplication(primarySources))
①整个run方法
②对整个Run方法进行注释说明
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
//记录程序运行时间
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// ConfigurableApplicationContext Spring 的上下文
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//1、获取并启动监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//2、构造应用上下文环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//处理需要忽略的Bean
configureIgnoreBeanInfo(environment);
//打印banner
Banner printedBanner = printBanner(environment);
//3、初始化应用上下文
context = createApplicationContext();
//实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//4、刷新应用上下文前的准备阶段
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//5、刷新应用上下文
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;
}
③拆解代码进行解读
StopWatch stopWatch = new StopWatch();
stopWatch.start();
---------------------------------------------------------------
StopWatch是位于org.springframework.util包下的一个工具类,
通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting()
---------------------------------------------------------------
创建所有 Spring 运行监听器并发布应用启动事件
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
---------------------------------------------------------------
此方法是准备运行时的环境
1、加载外部化配置的资源到environment;
2、触发ApplicationEnvironmentPreparedEvent事件
configureIgnoreBeanInfo(environment);
---------------------------------------------------------------
配置spring.beaninfo.ignore,并添加到名叫systemProperties的PropertySource中;默认为true即开启
Banner printedBanner = printBanner(environment);
---------------------------------------------------------------
打印banner
Banner printedBanner = printBanner(environment);
---------------------------------------------------------------
打印banner
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
---------------------------------------------------------------
刷新应用上下文前的准备阶段
refreshContext(context)
---------------------------------------------------------------
(重点重点!!!)
刷新应用上下文
afterRefresh(context, applicationArguments);
---------------------------------------------------------------
刷新应用上下文后的扩展接口
stopWatch.stop();
---------------------------------------------------------------
时间记录停止
listeners.started(context);
---------------------------------------------------------------
发布容器启动完成事件
④整个run方法分为六步
第一步:获取并启动监听器
第二步:构造应用上下文环境
第三步:初始化应用上下文
第四步:刷新应用上下文前的准备阶段
第五步:刷新应用上下文
第六步:刷新应用上下文后的扩展接口
后续spring源码系列文章会对这些方法进行详细解读
关注公众号
每周会更新干货知识