spring-boot初始化过程

54 篇文章 1 订阅

public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		/**
		 * 获取factory.properties下的所有配置SpringApplicationRunListener对象并封装为SpringApplicationRunListeners对象
		 */
		SpringApplicationRunListeners listeners = getRunListeners(args);
		//执行所有的SpringApplicationRunListener的starting方法
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			/**
			 * 加载所有的配置属性, application.properties,application.yml,和其他外部配置属性
			 * 执行spring.factories中的指定EnvironmentPostProcessor配置的环境初始化类如:EnvironmentPostProcessor
			 */
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
			//打印banner
			Banner printedBanner = printBanner(environment);
			//创建容器
			context = createApplicationContext();
			//创建异常报告器
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			/**
			 * 准备容器,组件对象之间进行关联,通知监听器准备执行容器初始化
			 * spring-cloud 实现自定义环境配置,
			 * 自定义的PropertySourceLocator实现环境变量设置。
			 */
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			//初始化容器-执行spring.factories中的指定EnableAutoConfiguration配置的@Configure的@Bean方法
			refreshContext(context);
			//初始化后之后执行,默认实现为空
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			//通知监听器容器初始化结束
			listeners.started(context);
			//调用执行ApplicationRunner  和 CommandLineRunner 的运行方法
			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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值