Spring Boot 应用启动流程

版权声明:Al1en欢迎转载 https://blog.csdn.net/u013310115/article/details/82931539

 基于Spring Boot2.0版本,本文主要大致描述启动流程。后面详细介绍流程中各个步骤的具体实现源码。首先我想给大家附上一张启动流程图,后续的文章都会根据图中各个节点展开。

目录

1.SpringApplication的run方法

2.创建应用上下文初始化类

3.加载刷新上下文

4.启动应用



1.SpringApplication的run方法

run方法包含着所有Spring Boot的秘密,它首先开启一个SpringApplicationRunListeners监听器,然后创建应用上下文ApplicationContext,通过上下文加载应用所需要的类和各种环境配置等,最后启动一个应用实例。一路跟进main方法中的run方法会看到如下代码清单。

public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();//开启监听
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);//启动看到的那个大大的Spring Boot,咱们可以自己改。
			context = createApplicationContext();//创建应用上下文初始化类(非常重要)
			exceptionReporters = getSpringFactoriesInstances(
					SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			refreshContext(context);//刷新上下文,其中包括启动内置tomcat等
			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;
	}

 

2.创建应用上下文初始化类

 继续跟进其中的createApplicationContext()方法我们看下是如何创建上下文初始化类,看下面代码清单。

/**
	 * 通过webApplicationType类型有三种可供选择的上下文加载类。
	 * 1.REACTIVE也就是2.0支持的webflux异步非阻塞请求,理论上适用高并发IO响应的场景。AnnotationConfigReactiveWebServerApplicationContext
	 * 2.SERVLET常用的web应MVC,AnnotationConfigServletWebServerApplicationContext
	 * 3.非web场景上下文加载类AnnotationConfigApplicationContext
	 * webApplicationType类型的设置默认在SpringApplication的构造函数中调用deduceWebApplicationType方法根据类路径中是否存在当前环境的class判断,也可通过手动设置。很尴尬的是手动设置我未曾使用过,但代码中存在设置方法。
	 */
	protected ConfigurableApplicationContext createApplicationContext() {
		Class<?> contextClass = this.applicationContextClass;
		if (contextClass == null) {
			try {
				switch (this.webApplicationType) {
				case SERVLET:
					contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
					break;
				case REACTIVE:
					contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
					break;
				default:
					contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
				}
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Unable create a default ApplicationContext, "
								+ "please specify an ApplicationContextClass",
						ex);
			}
		}
		return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
	}

3.加载刷新上下文

最终还是回到了Spring 的AbstractApplicationContext中的refresh()方法中。看代码清单如下

	protected void refresh(ApplicationContext applicationContext) {
		Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
		((AbstractApplicationContext) applicationContext).refresh();
	}

4.启动应用

在AbstractApplicationContext的refresh()方法中存在一个函数onRefresh(),如果是web环境在这会选择启动的servlet引擎。如果是普通的web应用而非reactive,则实际的onRefresh方法在AnnotationConfigServletWebServerApplicationContext的父类ServletWebServerApplicationContext中。代码如下

@Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();//创建webServer,如tomcat等
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

上面是整个Spring boot2.0的启动加载流程。未详细跟进代码细说,只是大致将整个流程捋一下。具体怎么做为什么这么做会在后面具体开篇章写。文中如有不对之处,欢迎及时指出。共同进步学习是我前进的动力,谢谢。

猜你喜欢

转载自blog.csdn.net/u013310115/article/details/82931539