ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-02-01 22:07:16  阅读:177  来源: 互联网

标签:上下文 启动 listeners 源码 ex context new 解析


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环境
			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;
	}

 

标签:上下文,启动,listeners,源码,ex,context,new,解析
来源: https://blog.csdn.net/sinat_32144323/article/details/113530009

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有