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

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

 

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

 

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

 

初始化基本流程

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值