(六)SpringBoot启动过程的分析-完成启动

-- 以下内容均基于2.1.8.RELEASE版本

紧接着上一篇(五)SpringBoot启动过程的分析-刷新ApplicationContext, 本文将分析上下文容器刷新完毕之后的流程。

在上下文容器刷新完毕之后,基本上就已经完成了SpringBoot的启动工作。

afterRefresh

刷新后的操作,是一个空的实现。

// SpringApplication.java

protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) {
}

记录应用启动耗时,并打印启动耗时日志

// SpringApplication.java

// 调用停止计时方法(先前已经调用过对应的Start方法)
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;
}

// 打印启动耗时日志
if (this.logStartupInfo) {
	new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}


发布上下文刷新完毕事件

// SpringApplication.java

listeners.started(context);

// EventPublishingRunListener.java
public void started(ConfigurableApplicationContext context) {
	context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context));
}

发布运行事件

// SpringApplication.java

public void running(ConfigurableApplicationContext context) {
	context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context));
}

至此,SpringBoot算是启动完毕。

posted @ 2021-04-06 20:17  不会发芽的种子  阅读(480)  评论(0)    收藏  举报