public ConfigurableApplicationContext run(String... args) {
// 用来测量时间
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 创建ConfigurableApplicationContext环境
ConfigurableApplicationContext context = null;
//创建数组用于异常报告的记录
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
// Headless模式是在缺少显示屏、键盘或者鼠标是的系统配置
configureHeadlessProperty();
//找到类路径下的META-INF/spring.factories下的所有ApplicationRunListeners,存入listeners数组里面
SpringApplicationRunListeners listeners = getRunListeners(args);
//调用所有listener的starting方法,开启监听
listeners.starting();
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//创建IOC环境,既舞台背景的搭建,并将listener绑定到environment里面
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
// 跳过搜索BeanInfo类
configureIgnoreBeanInfo(environment);
//打印那个banner图案
Banner printedBanner = printBanner(environment);
//创建ApplicationContext环境 既ioc容器
context = createApplicationContext();
//找到类路径下的META-INF/spring.factories下的所有SpringBootExceptionReporter,存入listeners数组里面
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);
//回调runner
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;
}
网友评论