Spring Boot 2.2.6 源码之旅六SpringApplication启动流程六

初始化基本流程

在这里插入图片描述

SpringApplication的prepareEnvironment准备环境

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments) {
		// Create and configure the environment
		ConfigurableEnvironment environment = getOrCreateEnvironment();//获取环境
		configureEnvironment(environment, applicationArguments.getSourceArgs());
		ConfigurationPropertySources.attach(environment);//ConfigurationPropertySources附加到环境一次
		listeners.environmentPrepared(environment);//广播环境准备好的事件
		bindToSpringApplication(environment);//绑定到SpringApplication
		if (!this.isCustomEnvironment) {
			environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
					deduceEnvironmentClass());//推断环境,如果不是这个类型,要进行切换
		}
		ConfigurationPropertySources.attach(environment);//又附加一次,主要是为了放最前面
		return environment;
	}

SpringApplication的getOrCreateEnvironment创建环境

根据前面的webApplicationType判断要创建哪种环境。

	private ConfigurableEnvironment getOrCreateEnvironment() {
		if (this.environment != null) {
			return this.environment;
		}
		switch (this.webApplicationType) {
		case SERVLET:
			return new StandardServletEnvironment();
		case REACTIVE:
			return new StandardReactiveWebEnvironment();
		default:
			return new StandardEnvironment();
		}
	}

configureEnvironment配置环境

这里主要是配置了很多的类型转换器和格式转换器,另外两个跟换进属性相关

	protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
		if (this.addConversionService) {
			ConversionService conversionService = ApplicationConversionService.getSharedInstance();//配置转换器
			environment.setConversionService((ConfigurableConversionService) conversionService);//设置到环境中去
		}
		configurePropertySources(environment, args);//配置默认属性
		configureProfiles(environment, args);//配置代码添加的环境
	}

ApplicationConversionService的getSharedInstance配置转换器

这个里面有配置很多转换器,我就不多讲了,自己看就行了。
在这里插入图片描述
在这里插入图片描述
其他剩下的暂时不讲,因为比较复杂,很多都是一些属性的配置,比如会获取你的spring.profiles.active信息,而且是ConfigFileApplicationListener收到环境准备好的事件后做的事,当然还有其他的一些配置信息,我们还是先把主线弄明白再搞细节,不过这里配置完成会进行广播,这次的事件是ApplicationEnvironmentPreparedEvent
在这里插入图片描述

SpringApplication的printBanner打印banner

内部支持 "gif", "jpg", "png","txt"
在这里插入图片描述
默认把资源放到resources下,名字为banner.xxx就可以了,自己可以去试试:
在这里插入图片描述
我们经常看到的默认的在这里面:
在这里插入图片描述

createApplicationContext创建上下文

根据类型创建对应的上下文对象,默认全是注解

	protected ConfigurableApplicationContext createApplicationContext() {
		Class<?> contextClass = this.applicationContextClass;
		if (contextClass == null) {
			try {
					switch (this.webApplicationType) {
				case SERVLET://AnnotationConfigServletWebServerApplicationContext
					contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
					break;
				case REACTIVE://AnnotationConfigReactiveWebServerApplicationContext
					contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
					break;
				default://AnnotationConfigApplicationContext
					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);
	}

在这里插入图片描述

准备环境这里比较复杂,可以写很多,这个还是细节的时候去看,先把脉络理完吧,后面讲上下文的准备,这个也很重要,准备完了就是spring的核心refresh了。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值