源码:Spring常规Bean创建过程

Bean创建过程:

一、版本

bash 复制代码
5.3.10

二、学习内容

bash 复制代码
Bean创建过程源码

三、Bean生命周期

时间轴地址:点击

四、bean创建过程脑图总结

脑图地址:点击

五、源码过程

bash 复制代码
说明:
bean创建入口一般都是通过getBean(xxx);方法进入的,进入后就会调用doGetBean方法,
咱们就直接从AbstractBeanFactory类的doGetBean开始
doGetBean()
java 复制代码
//1、执行getSingleton(beanName);从单例池里获取该bean,咱们新建肯定没有,直接往下
getSingleton(beanName);
//2、判断如果是单例直接执行(备注:本文只讲常规单例bean创建):
 getSingleton(beanName, () -> {
		try {
			return createBean(beanName, mbd, args);
		}
		catch (BeansException ex) {
			// Explicitly remove instance from	 singleton cache: It might have been put there
			// eagerly by the creation process, to allow for circular reference resolution.
			// Also remove any beans that received a temporary reference to the bean.
			destroySingleton(beanName);
			throw ex;
		}
	});
getSingleton(String beanName,ObjectFactory<?> singletonFactory)方法:
java 复制代码
//1、创建当前bean的BeanCurrentlyInCreationException对象;
beforeSingletonCreation(beanName); 
//2、实际创建,这里是用函数式(@FunctionalInterface)接口ObjectFactory<T>写的 
//.getObject() == createBean(); 【createBean()方法比较关键,后面细说】
singletonFactory.getObject();
//3、this.singletonsCurrentlyInCreation.remove(beanName)
afterSingletonCreation(beanName);
//4、将创建的单例bean放入单例池中:
//this.singletonObjects.put(beanName, singletonObject);
//this.singletonFactories.remove(beanName);	
//this.earlySingletonObjects.remove(beanName);			
//this.registeredSingletons.add(beanName);
addSingleton(beanName, singletonObject);
//5、创建完成返回Bean; 下面的内容主要是补充剖析第二点。
createBean()方法:
java 复制代码
//1、实例化前执行:这里主要执行所有实现了InstantiationAwareBeanPostProcessor的处理器,
//执行方法为:postProcessBeforeInstantiation();如果执行了上面方法获得了对象还会执行:
//postProcessAfterInitialization();方法最终返回bean对象。
resolveBeforeInstantiation(beanName, mbdToUse); 
//2、重点方法,详解在下面:
//1)、执行创建
//2)、属性注入
//3)、初始化 等
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
doCreateBean()方法
java 复制代码
//1、创建bean实例,这里边会有有一个获取构造方法的逻辑,这里就不多讲了,默认取无参构造。
createBeanInstance(beanName, mbd, args);
//2、实例化后执行BeanPostProcessor:
//执行所有MergedBeanDefinitionPostProcessor 类型beanPostProcessor的postProcessMergedBeanDefinition方法();
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
//3、将bean实例存入三级缓存中,这里对循环依赖有所有帮助的。
//一级缓存:singletonObjects
//二级缓存:earlySingletonObjects
//三级缓存:singletonFactories
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
//4、属性填充:主要依赖于bean后置处理器来完成
//4.1、属性填充执行BeanPostProcessor处理1:
//执行所有InstantiationAwareBeanPostProcessor类型的bean处理器postProcessAfterInstantiation()方法
//4.2、属性填充执行BeanPostProcessor处理2:
//执行所有InstantiationAwareBeanPostProcessor类型的bean处理器postProcessProperties()方法,
//这里有非常重要的处理AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor 处理器,会执行依赖注入功能。
populateBean(beanName, mbd, instanceWrapper);
//5、初始化
initializeBean(beanName, exposedObject, mbd);
//5.1、执行Aware(部分)接口实现的功能:
//BeanNameAware
//BeanClassLoaderAware
//BeanFactoryAware
invokeAwareMethods(beanName, bean);
//5.2、初始化前BeanPostProcessor​处理:
//获取所有实现BeanPostProcessor的处理器
//执行postProcessBeforeInitialization()方法
//特殊说明:这里有个ApplicationContextAwareProcessor处理器,主要处理部分Aware接口实现类的功能:
//EnvironmentAware
//EmbeddedValueResolverAware
//ResourceLoaderAware
//MessageSourceAware
//ApplicationStartupAware
//ApplicationEventPublisherAware
//ApplicationContextAware
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//5.3、初始化:
//1)、执行实现了InitializingBean类的afterPropertiesSet()方法初始化
//2)、执行init-method方法初始化
invokeInitMethods(beanName, wrappedBean, mbd);
//5.4、初始化后执行处理器:
//执行所有实现BeanPostProcessor的处理器postProcessAfterInitialization()方法;
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
相关推荐
麦兜*1 小时前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
程序员爱钓鱼3 小时前
Go语言实战案例-读取用户输入并打印
后端·google·go
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
Livingbody7 小时前
基于【ERNIE-4.5-VL-28B-A3B】模型的图片内容分析系统
后端
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
你的人类朋友8 小时前
🍃Kubernetes(k8s)核心概念一览
前端·后端·自动化运维