springboot源码解析(二) springboot启动过程

springboot启动入口类如下:

@SpringBootApplication
public class SpringBootApplication {

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

进入run方法:

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

在这个静态方法中,创建SpringApplication对象,并调用该对象的run方法。其中

new SpringApplication(primarySources)称之为SpringApplication准备阶段,run方法称之为SpringApplication运行阶段。

一、SpringApplication准备阶段

进入SpringApplication构造函数:

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
        //配置Spring Boot Bean 源
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        //推断 Web 应用类型
		this.webApplicationType = deduceWebApplicationType();
        //加载应用上下文初始器
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
       //加载应用事件监听器
		setListeners((Collection) 
                     getSpringFactoriesInstances(ApplicationListener.class));
        //推断引导类
		this.mainApplicationClass = deduceMainApplicationClass();
	}

1.推断 Web 应用类型

private WebApplicationType deduceWebApplicationType() {
		if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
				&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : WEB_ENVIRONMENT_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

根据当前应用 ClassPath 中是否存在相关实现类来推断 Web 应用的类型,包括:

Web Reactive: WebApplicationType.REACTIVE

Web Servlet: WebApplicationType.SERVLET

非 Web: WebApplicationType.NONE

2.加载应用上下文初始器和应用事件监听器

利用 Spring 工厂加载机制,实例化 ApplicationContextInitializer及ApplicationListener 实现类,并排序对象集合。

private  Collection getSpringFactoriesInstances(Class type,
			Class[] parameterTypes, Object... args) {
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set names = new LinkedHashSet<>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

3.推断引导类(Main Class)

private Class deduceMainApplicationClass() {
		try {
			StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
			for (StackTraceElement stackTraceElement : stackTrace) {
				if ("main".equals(stackTraceElement.getMethodName())) {
					return Class.forName(stackTraceElement.getClassName());
				}
			}
		}
		catch (ClassNotFoundException ex) {
			// Swallow and continue
		}
		return null;
	}

二、SpringApplication运行阶段

public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
        //加载 SpringApplication 运行监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
        //运行 SpringApplication 运行监听器
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
            //创建 Environment
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
            //创建 Spring 应用上下文
			context = createApplicationContext();
			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;
	}

 

你可能感兴趣的:(springboot源码解析(二) springboot启动过程)