前言
上章介绍了springboot入口类SpringApplication类的初始化流程,本章,我们分析介绍SpringApplication.run()方法的运行流程及原理。
SpringApplication实例run方法运行原理分析
public ConfigurableApplicationContext run(String... args) {
//1.创建并启动计时监控类
StopWatch stopWatch = new StopWatch();
stopWatch.start();
//2.初始化应用上下文和异常报告集合
ConfigurableApplicationContext context = null;
//异常通知者列表
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//3.设置系统配置java.awt.headless
configureHeadlessProperty();
//4.创建所有 Spring 运行监听器并发布应用启动事件
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
//5.初始化应用启动参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//6.根据运行监听器和应用参数来准备Spring环境变量
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
//7.创建 Banner 打印类
Banner printedBanner = printBanner(environment);
//8.创建应用上下文
context = createApplicationContext();
//9.获取Spring异常报告者Bean列表
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//10.准备应用上下文
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//11.刷新应用上下文
refreshContext(context);
//12.应用上下文刷新后置处理
afterRefresh(context, applicationArguments);
//13.停止计时监控类
stopWatch.stop();
//14.输出日志记录执行主类名、时间信息
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//15.发布应用上下文启动完成事件
listeners.started(context);
//16.执行所有 Runner 运行器
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
//处理运行异常
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//17.发布应用上下文就绪事件
listeners.running(context);
}
catch (Throwable ex) {
//处理运行异常
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
//18.返回应用上下文
return context;
}
run方法比较复杂,我们一步一步分析。
首先看第1步
创建并启动计时监控类
StopWatch stopWatch = new StopWatch();
stopWatch.start();
跳转到StopWatch来看下该类的实现
public void start() throws IllegalStateException {
start("");
}
public void start(String taskName) throws IllegalStateException {
if (this.currentTaskName != null) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
}
可以看出,start()首先记录了当前任务的名称,默认为空字符串,然后记录当前 Spring Boot 应用启动的开始时间。
第2步
初始化应用上下文和异常报告集合
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
再看第3步
设置系统属性 java.awt.headless 的值
configureHeadlessProperty();
设置该默认值为:true,表示运行在服务器端,在没有显示器和鼠标键盘的模式下工作,模拟输入输出设备功能。说白了就是当检测没有显示器和键盘鼠标时,也运行程序启动。
第4步
创建所有 Spring 运行监听器并发布应用启动事件
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
我们看下getRunListeners()的实现
private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(
SpringApplicationRunListener.class, types, this, args));
}
SpringApplicationRunListeners(Log log,
Collection<? extends SpringApplicationRunListener> listeners) {
this.log = log;
this.listeners = new ArrayList<>(listeners);
}
创建逻辑和之前实例化初始化器和监听器的一样,一样调用的是 getSpringFactoriesInstances 方法来获取配置的监听器名称并实例化所有的类。
SpringApplicationRunListener 所有监听器配置在 spring-boot-2.1.4.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
第5步
初始化默认应用参数类
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
第6步
根据运行监听器和应用参数来准备 Spring 环境
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
我们看下prepareEnvironment()的实现
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
//获取/创建应用环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
//配置应用环境
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
//获取/创建应用环境,这里分为标准 Servlet 环境和标准环境。
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
switch (this.webApplicationType) {
case SERVLET:
return new StandardServletEnvironment();
case REACTIVE:
return new StandardReactiveWebEnvironment();
default:
return new StandardEnvironment();
}
}
//配置应用环境
protected void configureEnvironment(ConfigurableEnvironment environment,
String[] args) {
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService
.getSharedInstance();
environment.setConversionService(
(ConfigurableConversionService) conversionService);
}
//1.配置 property sources
configurePropertySources(environment, args);
//2.配置 Profiles
configureProfiles(environment, args);
}
第7步
创建 Banner 打印类
Banner printedBanner = printBanner(environment);
第8步
创建应用上下文
context = createApplicationContext();
我们继续看下createApplicationContext()的实现,其实就是根据不同的应用类型初始化不同的上下文应用类。
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
第9步
准备异常报告器
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
逻辑和之前实例化初始化器和监听器的一样,一样调用的是 getSpringFactoriesInstances 方法来获取配置的异常类名称并实例化所有的异常处理类。
该异常报告处理类配置在 spring-boot-2.1.4.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。
# Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers
第10步
准备应用上下文
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
//绑定环境到上下文
context.setEnvironment(environment);
//配置上下文的 bean 生成器及资源加载器
postProcessApplicationContext(context);
//为上下文应用所有初始化器
applyInitializers(context);
//触发所有 SpringApplicationRunListener 监听器的 contextPrepared 事件方法
listeners.contextPrepared(context);
//记录启动日志
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
//注册两个特殊的单例bean
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
//加载所有资源
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
//触发所有 SpringApplicationRunListener 监听器的 contextLoaded 事件方法
listeners.contextLoaded(context);
}
第11步
刷新应用上下文
refreshContext(context);
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
第12步
应用上下文刷新后置处理
afterRefresh(context, applicationArguments);
//默认方法体是空,可以做一些自定义的后置处理操作。
protected void afterRefresh(ConfigurableApplicationContext context,
ApplicationArguments args) {
}
第13步
停止计时监控类
计时监听器停止,并统计一些任务执行信息。
stopWatch.stop();
public void stop() throws IllegalStateException {
if (this.currentTaskName == null) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.currentTaskName = null;
}
第14步
输出日志记录执行主类名、时间信息
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
第15步
发布应用上下文启动完成事件
listeners.started(context);
第16步
执行所有 Runner 运行器
callRunners(context, applicationArguments);
//callRunners实现
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList<>();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
for (Object runner : new LinkedHashSet<>(runners)) {
if (runner instanceof ApplicationRunner) {
callRunner((ApplicationRunner) runner, args);
}
if (runner instanceof CommandLineRunner) {
callRunner((CommandLineRunner) runner, args);
}
}
}
第17步
发布应用上下文就绪事件
触发所有 SpringApplicationRunListener 监听器的 running 事件方法。
listeners.running(context);
第18步
返回应用上下文
return context;
网友评论