Springboot启动流程分析-3——run方法概览

上一篇文章主要是介绍了创建SpringApplication的实例创建。实例创建完毕之后,就进入到了run方法中执行。这一篇主要对run方法进行概览。

run方法中主要包含如下内容

1.注册一个StopWatch,用于监控启动过程
2.获取监听器SpringApplicationRunListener,用于springboot启动过程中的事件广播
3.设置环境变量environment
4.创建spring容器
5.创建FailureAnalyzers错误分析器,用于处理记录启动过程中的错误信息
6.调用所有初始化类的initialize方法
7.初始化spring容器
8.执行ApplicationRunner和CommandLineRunner的实现类
9.启动完成

参考链接:https://blog.csdn.net/mozf881/article/details/84455359

/**
	 * 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 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;
	}

StopWatch 计时器

主要用于开启计时器,用于统计应用程序启动时间。

configureHeadlessProperty 无显模式

private void configureHeadlessProperty() {
		System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
				System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
	}

该方法只做了一件事:设置了一个名为java.awt.headless的系统属性,把属性从一个地方取出来,然后又设置到同一个地方,看起来有些冗余,其实这是因为System中的两个读写属性的方法不对等.

System中getProperty()有2个重载方法,但却只有一个setProperty()方法,其中getProperty()有单参和双参两方法,单参就是简单的获取属性,有就有,没有就没有,双参则聪明一点,在没有的时候会返回一个调用者指定的默认值,所以经过这样操作后,不管有没有那个属性,最终都能保证有.

所以先取后设.

这里主要是设置该应用程序,即使没有检测到显示器,也允许其启动.

#getRunListeners() 所有监听器启动

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

这里加载的是
/Users/yyrj/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.2.2.RELEASE/cc636f24a5ebbfb21f1c8c30ed9c3b13512c16ec/spring-boot-2.2.2.RELEASE.jar!/META-INF/spring.factories
在这里插入图片描述
所以这里加载后的SpringApplicationRunListeners是的listeners数组中包含了一个listener在这里插入图片描述
监听器启动

listeners.starting();

遍历启动每一个监听器,当然,这里是只有EventPublishingRunListener 这一个监听器

void starting() {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.starting();
		}
	}

在EventPublishingRunListener中启动,就是广播一个事件

@Override
	public void starting() {
		this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
	}

#ApplicationArguments 启动参数

ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

调试时候启动参数都是空的

2020年01月10日
witch_soya

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值