spring三级缓存源码分析
总体思路如下如所示
debug的源码过程
循环依赖debug
org.springframework.context.support.AbstractApplicationContext#refresh
finishBeanFactoryInitialization //LINE583, 实例化的入口
org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
beanFactory.preInstantiateSingletons(); //LINE918
org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons
for (String beanName : beanNames) { //LINE933, 准备实例化所有注册的beannames(循环依赖为题解决后,两个bean都实例化完成后需要回到这里)
getBean(beanName); //LINE955
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
Object sharedInstance = getSingleton(beanName); //尝试从缓存中获取,第一次因为一二三级缓存中都没有对象,所以返回null
sharedInstance = getSingleton(beanName, () -> { //LINE333-335, 再次尝试从缓存中拿数据,这次不一样的地方在于会尝试创建,lamda就是getObject的具体实现
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)
beforeSingletonCreation(beanName); //LINE227 做标记,将当前的beanName添加到singletonsCurrentlyInCreation集合
singletonObject = singletonFactory.getObject(); // LINE234,调用lambda的getObject方法创建bean,上述12行
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
return createBean(beanName, mbd, args); //LINE335, 创建bean user11
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
Object beanInstance = doCreateBean(beanName, mbdToUse, args); // LINE542 do开头的,实际‘干活的’
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
instanceWrapper = createBeanInstance(beanName, mbd, args);//LINE582 对于debug 循环依赖来说,没必要跟进去,具体内容为反射创建bean
// 得到创建的结构, User对象 ---- User@2482
earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && // LINE606-607, 是否提前暴露标志位
isSingletonCurrentlyInCreation(beanName));
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); // LINE613, 添加三级缓存,此处的value为lambda表达式,当尝试获取三级缓存对象的时候,
// 调用其getObject方法, 则会执行当前的getEarlyBeanReference方法(返回当前已经初始化好的bean ---- User@2482)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingletonFactory //添加三级缓存 {key: user11, vlaue: lambda表达式}
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
populateBean(beanName, mbd, instanceWrapper); //LINE619,为当前对象添加属性值
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean
applyPropertyValues(beanName, mbd, bw, pvs); //LINE1452, 准备添加属性
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues
for (PropertyValue pv : original) { //LINE1693, 遍历当前bean的属性列表,并逐一添加,当遍历到引用属性(依赖问题)时,会进入到下一次getBean的大循环中
pv.setConvertedValue(convertedValue);//LINE1725, 普通属性在此处赋值
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); // LINE1710
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveValueIfNecessary
RuntimeBeanReference ref = (RuntimeBeanReference) value //LINE112 将依赖强转为runtimereference类型,此时依赖的pet为null
return resolveReference(argName, ref); //LINE113,解决依赖的入口
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveReference
bean = this.beanFactory.getBean(resolvedName); //LINE330 解决依赖,开始实例化依赖的bean对象,dog1,初始化完成后会回到这里接着初始化当前的bean User@2482
// user11 找 dog1
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
Object sharedInstance = getSingleton(beanName); //尝试从缓存中获取,第一次因为一二三级缓存中都没有对象,所以返回null
sharedInstance = getSingleton(beanName, () -> {
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)
beforeSingletonCreation(beanName); //LINE227 做标记,将当前的beanName添加到singletonsCurrentlyInCreation集合
singletonObject = singletonFactory.getObject(); // LINE234,调用lambda的getObject方法创建bean dog1,上述45行
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
return createBean(beanName, mbd, args); //LINE335, 创建bean
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
Object beanInstance = doCreateBean(beanName, mbdToUse, args); // LINE542 do开头的,实际‘干活的’
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
instanceWrapper = createBeanInstance(beanName, mbd, args);//LINE582 对于debug 循环依赖来说,没必要跟进去,具体内容为反射创建bean,
// 得到创建的结果, Dog@3299
earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && // LINE606-607, 是否提前暴露标志位
isSingletonCurrentlyInCreation(beanName));
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); // LINE613,此处的value为lambda表达式,当尝试获取三级缓存对象的时候,
// 调用其getObject方法, 则会执行当前的getEarlyBeanReference方法(对于一般情况,会返回当前已经初始化好的bean ---- Dog@3299)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingletonFactory
//添加完三级缓存 key: dog1, vlaue: lambda表达式, 此时三级缓存中有{user11: lambda, dog1:lambda}
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
populateBean(beanName, mbd, instanceWrapper); //LINE619,为当前对象添加属性值
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean
applyPropertyValues(beanName, mbd, bw, pvs); //LINE1452, 准备添加属性
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues
for (PropertyValue pv : original) { //LINE1693, 遍历当前bean的属性列表,并逐一添加,当遍历到引用属性(依赖问题)时,会进入到下一次getBean的大循环中
pv.setConvertedValue(convertedValue);//LINE1725, 普通属性在此处赋值
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); // LINE1710
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveValueIfNecessary
RuntimeBeanReference ref = (RuntimeBeanReference) value //LINE112 将依赖强转为runtimereference类型,此时依赖的master为null
return resolveReference(argName, ref); //LINE113,解决依赖的入口
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveReference
bean = this.beanFactory.getBean(resolvedName); //LINE330 解决依赖,开始获取实例化依赖的bean对象,user11,获取后会回到这里继续初始化Dog@3299
// dog1找user11
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
Object sharedInstance = getSingleton(beanName); //尝试从缓存中获取
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, boolean)
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); //LINE192, 从三级缓存中获取到了key为 user11的对象
//【因为第一阶段设置的属性值还没有最终deepCOpy到user对象中,所以此时获取的三级缓存对象User@2482属性值都是空】
singletonObject = singletonFactory.getObject(); //三级缓存中获取到了对象 User@2482, 于是将其从三级缓存中删除,放到二级缓存中,
// 此时三级缓存中有{dog1: lambda}, 二级缓存中有{user11: User@2482}
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null); //返回对象User@2482
//回到上述73行,此时已经拿到了半成品User对象,
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveReference
bean = this.beanFactory.getBean(resolvedName); //此时拿到了User@2482{属性值都是null} //回到dog1的属性赋值流程
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveValueIfNecessary
return resolveReference(argName, ref); //LINE113,返回对象User@2482
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues //回到这里继续完成Dog对象的初始化
for (PropertyValue pv : original) { //LINE1693, 遍历当前bean的属性列表,并逐一添加,当遍历到引用属性(依赖问题)时,会进入到下一次getBean的大循环中
pv.setConvertedValue(convertedValue);//LINE1725, 普通属性在此处赋值
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); // LINE1710, 此时拿到了resolvedValue对象 User@2482
deepCopy.add(new PropertyValue(pv, convertedValue)); //LINE1730, 将对象属性值添加到属性对象PropertySource,并且添加到listdeepCopy这个list里面
bw.setPropertyValues(new MutablePropertyValues(deepCopy)); //LINE1740, 这里所做的操作就是将对象属性list全部赋值到BeanWraper对象,此时对象Dog已经完成了全部属性的赋值工作
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
populateBean(beanName, mbd, instanceWrapper); //LINE619,此时populatebean已经完成,接着完成其余的初始化步骤,
// aware接口,
// BeanPostProcessor#beforeInitialization
//init方法 isInitializingBean? invokeAfterPropertiesSet
// init方法 是否配置了init-method? invokeCustomInitMethod
// BeanPostProcessor#afterInitialization
exposedObject = initializeBean(beanName, exposedObject, mbd); //至此,dog对象的初始化步骤已经完成,可以使用了exposeObject
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
Object beanInstance = doCreateBean(beanName, mbdToUse, args); //LINE542, 返回dog对象
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
sharedInstance = getSingleton(beanName, () -> { // LINE333
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)
singletonObject = singletonFactory.getObject(); // LINE234, 返回创建的dog对象
afterSingletonCreation(beanName); //LINE257, 移除正在创建的标志位
addSingleton(beanName, singletonObject); //LINE260,接着会将dog1添加到一级缓存并将其相关对象移除三级缓存
//回到user11的属性赋值流程中,就快完了,坚持
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveValueIfNecessary
return resolveReference(argName, ref); //返回dog对象
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues
bw.setPropertyValues(new MutablePropertyValues(deepCopy));//LINE1740,user11属性赋值
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
populateBean(beanName, mbd, instanceWrapper);//LINE619, user11赋值操作完成
紧接着完成剩余初始化操作,拿着引用操作对象,和二级缓存里面的对象是同一个
//initializeBean
Object earlySingletonReference = getSingleton(beanName, false); //从二级缓存中拿到了user对象
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
} //LINE635-637,判断是否是同一个bean,将缓存中的对象赋值给当前对象,没有区别
return exposedObject; //返回user11对象
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
sharedInstance = getSingleton(beanName, () -> { // LINE333
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)
singletonObject = singletonFactory.getObject(); // LINE234, 返回创建的dog对象
afterSingletonCreation(beanName); //LINE257, 移除正在创建的标志位
addSingleton(beanName, singletonObject); //LINE260,接着会将user11添加到一级缓存并将其相关对象移除二级缓存
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); //LINE345
return adaptBeanInstance(name, beanInstance, requiredType); // LINE398, 返回user11对象
返回for (String beanName : beanNames) { //完成所有bean的初始化
至此finishRefresh();完成容器的加载