springboot的启动流程

一.总结
springboot的启动流程分下面几步走:
1.初始化
2.获取监听器并启动监听器
3.构造容器环境
4.创建容器
5.实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
6.准备容器
7.刷新容器
8.刷新容器后置处理

public ConfigurableApplicationContext run(String... args) {
		//时间监控
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		//java.awt.headless是J2SE的一种模式用于在缺少显示屏、键盘或者鼠标时的系统配置,很多监控工具如jconsole 需要将该值设置为true,系统变量默认为true
		configureHeadlessProperty();
		//获取spring.factories中的监听器变量,args为指定的参数数组,默认为当前类SpringApplication
		//第一步:获取并启动监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
			//第二步:构造容器环境
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			//设置需要忽略的bean
			configureIgnoreBeanInfo(environment);
			//打印banner
			Banner printedBanner = printBanner(environment);
			//第三步:创建容器
			context = createApplicationContext();
			//第四步:实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
			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);
			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;
	}



再来看每一步都做了那些事情:
1.初始化的过程
A.根据加载器中是否包含了某些特定的类来判断容器类型,在创建容器和容器环境的时候就会被用到。
B.初始化监听器,利用SpringFactoriesLoader读取依赖jar下面的所有META-INF/spring.factories文件,获取SpringApplicationRunListener对应的监听器全限定名称,并利用反射初始化。
C.初始化ApplicationContextInitializer对应的Initializer对象。
D.找到main方法所在的类,寻找启动类

2.获取监听器并启动监听器
A.获取META-INF/spring.factories文件中SpringApplicationRunListener对应的所有监听器,初始化它们或得一个监听器集合,将这个集合负值给SpringApplicationRunListeners,SpringApplicationRunListeners相当于是一个监听器的广播器,要发布什么事件都是先由SpringApplicationRunListeners来发布,然后再遍历所有的监听器发布事件。
B.监听器开始监听,这里会创建一个线程池,通过异步的方式来发送事件。

3.构造容器环境
A.根据容器类型来创建不同容器环境对象。
B.加载系统变量和环境变量。
C.监听器发布环境准备事件,这个里面有一个很重要的监听器ConfigFileApplicationListener,来加载项目的配置文件。

4.创建容器
A.根据初始化时获取到的容器类型来获取对应的容器。当容器类型是SERVLET的时候,获取到的是AnnotationConfigServletWebServerApplicationContext容器

5.实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误,在try ,cachez中,启动报错的时候,异常会被cach捕获,所以SpringBootExceptionReporter在cache中起作用的。

6.准备容器
A.设置容器环境,将创建好的容器环境赋值给容器
B.执行容器中的ApplicationContextInitializer
C.向各监听器发送容器准备好了的事件
D.向容器中注册容器启动参数
E.将启动类注册到容器中
F.发布容器已加载事件

7.刷新容器
A.刷新上下文环境,初始化上下文环境,对系统的环境变量或者系统属性进行准备和校验
B.初始化BeanFactory,解析XML,和填充准备BeanFactory
C.实例化所有剩余的(non-lazy-init) 单例Bean.
这篇文章比较详细的刷新容器所做的事情,但是排版比较差,以后有时间整理一下
https://bbs.csdn.net/topics/392397556?page=1#post-409544506

ps:要开始准备找工作了,北京的天气也越来越冷。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章