springboot启动源码解析(二):启动的整体流程解析

springboot启动的整体流程解析

在SpringApplication初始化(详见:springboot启动源码解析(一):SpringApplication初始化)之后,开始了真正意义上的启动过程,它的整个启动过程在SpringApplication的run()方法中。

	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

	public ConfigurableApplicationContext run(String... args) {
        // 首先初始化一个计时器,并开始了启动计时
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
        // 初始化一个启动上下文
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
        // 这一步是为了设置系统参数:java.awt.headless(在系统可能缺少显示设备、键盘或鼠标这些外设的情况下可以使用该模式,例如Linux服务器)
		configureHeadlessProperty();
        // listeners中维护着一个监听器列表,它们贯穿了整个启动过程,负责在各个启动步骤完成后发布相应事件事
        // listeners中维护的监听器列表是通过调用getSpringFactoriesInstances方法,从spring.factories中获取得到的类型为SpringApplicationRunListener的实例列表,但此列表中有且仅有一个事件发布监听器:EventPublishingRunListener
		SpringApplicationRunListeners listeners = getRunListeners(args);
        // 此处发布springboot开始启动的事件
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
            // 初始化应用参数,此参数从启动命令中进行解析,例如java -jar --spring.profiles.active=prod等
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 准备spring环境,传入listeners是为了准备环境时发布某些事件
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            // 配置需要忽略的Bean信息,该信息从应用环境中"spring.beaninfo.ignore"属性取得,如果未取到,则将默认值TRUE设置到System的"spring.beaninfo.ignore"属性中
			configureIgnoreBeanInfo(environment);
            // 获取并打印banner,并返回生成的PrintedBanner实例
			Banner printedBanner = printBanner(environment);
            // 创建应用上下文
			context = createApplicationContext();
            // 为此应用上下文设置applicationStartup,使应用程序上下文在启动期间能够记录一些指标,applicationStartup = ApplicationStartup.DEFAULT
			context.setApplicationStartup(this.applicationStartup);
            // 准备应用程序上下文,该步骤包含一个非常关键的操作:将启动类注入容器,为后续开启自动化提供基础
			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            // 刷新应用上下文,在此步骤中,进行了类扫描,bean注入等关键操作
			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, listeners);
			throw new IllegalStateException(ex);
		}

		try {
            // 发布应用上下文就绪事件
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
        // 返回应用上下文
		return context;
	}

可以看出,在springboot启动时,主要做了以下事件:

1、初始化启动上下文

2、初始化一个监听器列表listeners,listeners中只包含了一个监听器:EventPublishingRunListener,它主要负责发布启动中的各种事件

3、向listeners发布【开始启动】事件

4、准备springboot环境

5、创建应用上下文

6、对应用上下文做一些准备,该步骤包含一个非常关键的操作:将启动类注入容器,为后续开启自动化提供基础

7、刷新应用上下文,在此步骤中,进行了类扫描,bean注入等关键操作

8、向listeners发布【应用上下文已经启动】事件

9、执行所有Runner运行期

10、发布【应用上下文就绪】事件

11、返回应用上下文

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值