Spring三级缓存解决循环引用-时序图
2021-02-04 15:14:17 12 举报
Spring三级缓存解决循环引用,debug梳理了所有的步骤
作者其他创作
大纲/内容
getSingleton(beanName)
将a从三级缓存删除放入二级缓存
return a的对象
instantiationStrategy : SimpleInstantiationStrategy
开始逐个处理配置类
beanFactory : DefaultListableBeanFactory
return T
return b
BeanUtils
return NULL
return instanceWrapper
BeanUtils.instantiateClass(constructorToUse)
this.beanFactory.getBean(resolvedName)
将a对象从二级缓存删除放入到一级缓存
valueResolver : BeanDefinitionValueResolver
开始处理b的依赖属性a
populateBean执行完毕
自定义Bean的构造
getBean(beanName)
doCreateBean执行完毕,返回b对象
将b放入一级缓存
getObject()执行完毕
接下来重复循环加载下一个类,也就是b,会直接从一级缓存中读取
AbstractBeanFactory
getObject执行完毕
singletonFactory.getObject()
doGetBean(name)执行完毕
return null
将a放入三级缓存
package com.spring.debug.l3.cache.bean;public class B { private String nameB; private int ageB; private A a; public B(){ System.out.println(\"======初始化B......\"); } public String getNameB() { return nameB; } public void setNameB(String nameB) { this.nameB = nameB; } public int getAgeB() { return ageB; } public void setAgeB(int ageB) { this.ageB = ageB; } public A getA() { return a; } public void setA(A a) { this.a = a; }}
构造b对象
DefaultSingletonBeanRegistry
doCreateBean执行完毕
createBean执行完毕
开始处理a的依赖属性b
getBean(beanName)执行完毕
ClassPathXmlApplicationContext
构造a对象
AbstractApplicationContext
构造A对象
refresh()
return b对象
finishBeanFactoryInitialization(beanFactory)
DEBUG数据说明
return b对象,sharedInstance接收
package com.spring.debug.l3.cache.bean;public class A { private String nameA; private int ageA; private B b; public A(){ System.out.println(\"======初始化A......\"); } public String getNameA() { return nameA; } public void setNameA(String nameA) { this.nameA = nameA; } public int getAgeA() { return ageA; } public void setAgeA(int ageA) { this.ageA = ageA; } public B getB() { return b; } public void setB(B b) { this.b = b; }}
return b对象,singletonObject接收
: ObjectFactory
beanFactory.preInstantiateSingletons()
doGetBean执行完毕
new ClassPathXmlApplicationContext()
this.beanFactory.getBean(resolvedName)执行完毕,bean接收返回b对象
return beanInstance
返回构造的对象
返回对象a
this.beanFactory.getBean(resolvedName)执行完毕
a的属性注入完毕,继续处理a
Spring版本:5.2.4.RELEASExml配置:<?xml version=\"1.0\" encoding=\"UTF-8\"?><beans xmlns=\"http://www.springframework.org/schema/beans\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\"> <bean id=\"a\" class=\"com.spring.debug.l3.cache.bean.A\"> <property name=\"b\" ref=\"b\" /> </bean> <bean id=\"b\" class=\"com.spring.debug.l3.cache.bean.B\" > <property name=\"a\" ref=\"a\" /> </bean></beans>测试代码 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(\"spring/3L-cache-configration.xml\"); A a = (A) applicationContext.getBean(\"a\"); B b = (B) applicationContext.getBean(\"b\");
ClientSpringL3Cache
AbstractAutowireCapableBeanFactory
将b对象从三级缓存删除放入到一级缓存
return a对象,sharedInstance接收
applyPropertyValuess执行完毕
getSingleton执行完毕
addSingleton执行完毕
ctor.newInstance(argsWithDefaultValues)
return Object
从二级缓存中获取a对象并返回
至此,a、b对象已经放在一级缓存
收藏
0 条评论
下一页