【初识SpringBoot核心源码之SpringApplication构造器以及run方法主线流程-3】

一.知识回顾

【0.SpringBoot专栏的相关文章都在这里哟,后续更多的文章内容可以点击查看】
【1.SpringBoot初识之Spring注解发展流程以及常用的Spring和SpringBoot注解】
【2.SpringBoot自动装配之SPI机制&SPI案例实操学习&SPI机制核心源码学习】
【3.详细学习SpringBoot自动装配原理分析之核心流程初解析-1】
【4.详细学习SpringBoot自动装配原理之自定义手写Starter案例实操实战-2】
【5.IDEA中集成SpringBoot源码环境详细步骤讲解】

二.SpringBoot源码主线分析

SpringBoot源码的源码不可能通过一篇文章就搞定的,本文我们就来分析下SpringBoot源码中的主线流程。先掌握SpringBoot项目启动的核心操作,然后我们再深入每一个具体的实现细节。
注:本系列源码都以SpringBoot2.2.5.RELEASE版本来进行学习

2.1 SpringBoot启动的入口

启动一个SpringBoot项目的时候,入口程序就是main方法,而在main方法中就执行了一个run方法。

@SpringBootApplication
public class StartApp{

	public static void main(String[] args) {
		SpringApplication.run(StartApp.class);
	}
}

2.2 run方法

进入run()方法

/**
 * Static helper that can be used to run a {@link SpringApplication} from the
 * specified source using default settings. 666
 * @param primarySource the primary source to load
 * @param args the application arguments (usually passed from a Java main method)
 * @return the running {@link ApplicationContext}
 */
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
	// 调用重载的run方法,将传递的Class对象封装为了一个数组
	return run(new Class<?>[] { primarySource }, args);
}

调用了重载的一个run()方法,将我们传递进来的类对象封装为了一个数组,仅此而已。我们再进入run()方法。

/**
* Static helper that can be used to run a {@link SpringApplication} from the
 * specified sources using default settings and user supplied arguments.
 * @param primarySources the primary sources to load
 * @param args the application arguments (usually passed from a Java main method)
 * @return the running {@link ApplicationContext}
 */
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
	//这个地方就分为了俩个部分,一个是创建了SpringApplication对象,另外一部分就是调用它的run方法
	// 创建了一个SpringApplication对象,并调用其run方法
	// 1.先看下构造方法中的逻辑
	// 2.然后再看run方法的逻辑
	return new SpringApplication(primarySources).run(args);
}

在该方法中创建了一个SpringApplication对象。同时调用了SpringApplication对象的run方法。这里的逻辑有分支,先看SpringApplication的构造方法中的逻辑,然后再学习对应的run方法。

2.3 SpringApplication构造器

先进入SpringApplication的构造方法,发现它又调用了自身其它的构造方法

/**
* Create a new {@link SpringApplication} instance. The application context will load
 * beans from the specified primary sources (see {@link SpringApplication class-level}
 * documentation for details. The instance can be customized before calling
 * {@link #run(String...)}.
 * @param primarySources the primary bean sources
 * @see #run(Class, String[])
 * @see #SpringApplication(ResourceLoader, Class...)
 * @see #setSources(Set)
 */
public SpringApplication(Class<?>... primarySources) {
	// 调用其他的构造方法
	this(null, primarySources);
}

然后进入SpringApplication的构造方法

/**
 * Create a new {@link SpringApplication} instance. The application context will load
 * beans from the specified primary sources (see {@link SpringApplication class-level}
 * documentation for details. The instance can be customized before calling
 * {@link #run(String...)}.
 * @param resourceLoader the resource loader to use
 * @param primarySources the primary bean sources
 * @see #run(Class, String[])
 * @see #setSources(Set)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	// 传递的resourceLoader为null
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");
	// 记录主方法的配置类名称
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	// 记录当前项目的类型
	this.webApplicationType = WebApplicationType.deduceFromClasspath();
	// 加载配置在spring.factories文件中的ApplicationContextInitializer对应的类型并实例化
	// 并将加载的数据存储在了 initializers 成员变量中。
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
	// 初始化监听器 并将加载的监听器实例对象存储在了listeners成员变量中
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
	// 反推main方法所在的Class对象 并记录在了mainApplicationClass对象中
	this.mainApplicationClass = deduceMainApplicationClass();
}

SpringApplication构造器方法中完成了几个核心操作

  1. 推断当前项目的类型
  2. 加载配置在spring.factories文件中的ApplicationContextInitializer中的类型并实例化后存储在了initializers中。
  3. 和第2步骤一样,完成监听器的初始化操作,并将实例化的监听器对象存储在了listeners成员变量中
  4. 通过StackTrace反推main方法所在的Class对象

2.4.然后再进入run方法

再进入到SpringApplication.run()方法中。

/**
 * Run the Spring application, creating and refreshing a new
 * {@link ApplicationContext}.
 * @param args the application arguments (usually passed from a Java main method)
 * @return a running {@link ApplicationContext}
 */
public ConfigurableApplicationContext run(String... args) {
	// 创建一个任务执行观察器
	StopWatch stopWatch = new StopWatch();
	// 开始执行记录执行时间
	stopWatch.start();
	// 声明 ConfigurableApplicationContext 对象
	ConfigurableApplicationContext context = null;
	// 声明集合容器用来存储 SpringBootExceptionReporter 启动错误的回调接口
	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
	// 设置了一个名为java.awt.headless的系统属性
	// 其实是想设置该应用程序,即使没有检测到显示器,也允许其启动.
	//对于服务器来说,是不需要显示器的,所以要这样设置.
	configureHeadlessProperty();
	// 获取 SpringApplicationRunListener 加载的是 EventPublishingRunListener
	// 获取启动时的监听器---》 事件发布器  发布相关事件的
	SpringApplicationRunListeners listeners = getRunListeners(args);
	// 触发启动事件  发布 starting 事件 --》 那么监听starting事件的监听器就会触发
	listeners.starting();
	try {
		// 构造一个应用程序的参数持有类
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		// 创建并配置环境
		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
		// 配置需要忽略的BeanInfo信息
		configureIgnoreBeanInfo(environment);
		// 输出的Banner信息
		Banner printedBanner = printBanner(environment);
		// 创建应用上下文对象
		context = createApplicationContext();
		// 加载配置的启动异常处理器
		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
				new Class[] { ConfigurableApplicationContext.class }, context);
		// 刷新前操作
		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
		// 刷新应用上下文 完成Spring容器的初始化
		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);
	}
	// 返回上下文对象--> Spring容器对象
	return context;
}

在这个方法中完成了SpringBoot项目启动的很多核心的操作,我们来总结下上面的步骤

  1. 创建了一个任务执行的观察器,统计启动的时间
  2. 声明ConfigurableApplicationContext对象
  3. 声明集合容器来存储SpringBootExceptionReporter即启动错误的回调接口
  4. 设置java.awt.headless的系统属性
  5. 获取我们之间初始化的监听器(EventPublishingRunListener),并触发starting事件
  6. 创建ApplicationArguments这是一个应用程序的参数持有类
  7. 创建ConfigurableEnvironment这时一个配置环境的对象
  8. 配置需要忽略的BeanInfo信息
  9. 配置Banner信息对象
  10. 创建对象的上下文对象
  11. 加载配置的启动异常的回调异常处理器
  12. 刷新应用上下文,本质就是完成Spring容器的初始化操作
  13. 启动结束记录启动耗时
  14. 完成对应的事件广播
  15. 返回应用上下文对象。

好了,到这里【初识SpringBoot核心源码之SpringApplication构造器以及run方法主线流程-3】就学习到这里,到此SpringBoot项目的启动初始化的代码的主要流程就介绍完成了。细节部分后面详细讲解。敬请期待。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

硕风和炜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值