跟我一起阅读SpringBoot源码(二)——SpringApplication启动

前情回顾

在上一篇中我们看了springApplication实例的初始化过程,在实例化的时候,先是设置sources来源位置,然后根据classpath设置了应用类型,然后添加了写初始化器和监听器,最后设置了他的启动类是哪个。接下来我们看看SpringApplication的启动过程,下面一定比初始化过程复杂,慢慢看,看多少算多少,fighting~~~~~~~~~~

springApplication.run(args)

上面初始化感觉都好多过程,下面run肯定更复杂,加油,坚持!!!

/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * 运行spring应用,创建和刷新一个新的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 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);
			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;
	}

在这里插入图片描述
大概画了下springApplication启动过程。比较复杂,为了结构稍微好看点,分了多篇篇幅来写,比较简单的地方就没有另开篇幅。

StopWatch

StopWatch,懒得自己翻译了,直接看工具翻译的吧:
在这里插入图片描述
简单点就是说StopWatch就是一个计时器,get一个新技能。
在这里插入图片描述
demo一下,发现hutool里面也有StopWatch,哈哈:
在这里插入图片描述

configureHeadlessProperty

这里我真是没看懂在干嘛,这貌似涉及到jdk的知识了,贴个代码先撤吧。
private void configureHeadlessProperty() { System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless))); }

SpringApplicationRunListeners

SpringApplicationRunListeners listeners = getRunListeners(args);

private SpringApplicationRunListeners getRunListeners(String[] args) {
		Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
		return new SpringApplicationRunListeners(logger,
				getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
	}

返回一个SpringApplicationRunListeners,我们上回看过getSpringFactoriesInstances是在实例化工厂对象,这里就先不深究了,debug看下返回了个啥:
在这里插入图片描述
SpringApplicationRunListeners的实例,里面包含一个LogAdapter实例和一个EventPublishingRunListener,log实例就先不管了,反正就是拿来写日志的,先看看EventPublishingRunListener吧:
在这里插入图片描述

SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();

获取到监听器后立马给启动了,看下启动过程:

	void starting() {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.starting();
		}
	}
	@Override
	public void starting() {
		this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
	}

启动的时候发布了一个ApplicationStartingEvent事件,看下这个事件的监听则有哪些:
在这里插入图片描述
这些侦听者在ApplicationStartingEvent触发时干了什么?详见:跟我一起阅读SpringBoot源码(十)——侦听器

ApplicationArguments

在这里插入图片描述
这里是初始化的一个默认参数,我们看看默认参数有啥:

public DefaultApplicationArguments(String... args) {
		Assert.notNull(args, "Args must not be null");
		this.source = new Source(args);
		this.args = args;
	}

通过main方法输入的args来生成一个Source,然后把参数存起来,我们一般输入的args是{},那生成的默认参数是怎样的呢?debug下:
在这里插入图片描述

ConfigurableEnvironment

跟我一起阅读SpringBoot源码(三)——ConfigurableEnvironment

Banner

跟我一起阅读SpringBoot源码(六)——Banner

ConfigurableApplicationContext

跟我一起阅读SpringBoot源码(七)——上下文初始化

SpringBootExceptionReporter

上下文创建后,根据上下文有实例化了一个SpringBoot异常报告对象:

Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);

在这里插入图片描述
这里就不进去看了,应该是获取了一些异常报告对象。
在这里插入图片描述

准备上下文

跟我一起阅读SpringBoot源码(八)——准备应用上下文

刷新上下文

上下文刷新后处理

【啃不动了,今天就到这里吧,先歇下,未完待续……】

猜你喜欢

转载自blog.csdn.net/qq1049545450/article/details/112977838