Spring循环依赖的原理解析

1、什么是循环依赖?

​ 我们使用Spring的时候,在一个对象中注入另一个对象,但是另外的一个对象中也包含该对象。如图:

在Student中包含了teacher的一个属性;

在Teacher中包含有student的属性。这样就形成了一个循环依赖。

2、代码描述

xml配置文件

testCycle.java
private static void testCycle(){ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("cycle.xml");Teacher teacher = applicationContext.getBean(Teacher.class);System.out.println(teacher);Student student = applicationContext.getBean(Student.class);System.out.println(student);}public static void main(String[] args) {testCycle();}
Student.java
public class Student { private Teacher teacher; public Teacher getTeacher() {return teacher; } public void setTeacher(Teacher teacher) {this.teacher = teacher; }}
Teacher.java
public class Teacher { private Student student; public Student getStudent() {return student; } public void setStudent(Student student) {this.student = student; }}

3、 测试结果

此处输出的teacher中包含有student对象,student对象中也包含有teacher对象,且包含的对象都是不为null的。

4、为什么能够循环依赖解释

先给出一张图

在Spring创建bean的时候肯定也是一个一个去创建的。首先肯定会先去走一个(Teacher/Student)生命周期。这里以Teacher为例,当Spring去getBean(teacher)的时候,首先会去容器中获取,获取不到就会去创建teacher,当teacher创建完成后,会给teacher的属性(student)赋值,实际上容器中没有student对象,这时候也会去创建student对象,当student创建的时候会去给student中的teacher属性赋值,teacher之前已经创建过了,此时去getBean(teacher)是能够拿到的(注意:此时的teacher中student属性并没有赋值),这样student就创建完成了,那么就会回到teacher的student属性赋值的步骤,此时student已经创建是可以用getBean()拿到的,这样teacher对象就创建完毕了。然后回到第一步去创建student对象,这里student对象在创建teacher的时候就已经创建,可以直接使用getBean()获取到。给student中的属性赋值的时候也是一样,能够直接获取到teacher。自此循环依赖就已经结束了。

5、疑问

  1. 当我在给Teacher属性student的赋值的时候是怎么去getBean()的?
  2. 当给student中属性teacher赋值的时候getBean()为什么能够取到teacher?
  3. 为什么获取到的teacher属性是为完成注入的?

6、源码解释

整体的方法线

先看看源码:

getBean()->doGetBean()

getBean()->doGetBean()实际上是doGetBean再去获取bean对象

public  T getBean(String name, @Nullable Class requiredType, @Nullable Object... args)throws BeansException { return doGetBean(name, requiredType, args, false);}/** * Return an instance, which may be shared or independent, of the specified bean. * 返回指定 bean 的一个实例,该实例可以是共享的,也可以是独立的。 */@SuppressWarnings("unchecked")protected  T doGetBean(String name, @Nullable Class requiredType, @Nullable Object[] args, boolean typeCheckOnly)throws BeansException { // 转换beanName,FactoryBean的情况下beanName为&beanName,这里就是去掉&符号 String beanName = transformedBeanName(name); Object beanInstance; // Eagerly check singleton cache for manually registered singletons. // 急切检查单例缓存从手动创建的单例中,获取bean判断是否存在当前beanName的bean Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) {if (logger.isTraceEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) {... 省略代码...}beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else {// Fail if we're already creating this bean instance:// We're assumably within a circular reference.// 没有获取到,如果已经创建bean的实例,我们在一个循环引用中。当前的bean是否为正在创建中if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName);}// Check if bean definition exists in this factory.// 检查该工厂中是否存在bean的定义BeanFactory parentBeanFactory = getParentBeanFactory(); ... 省略代码...if (!typeCheckOnly) { // 标记bean已经创建,正在创建 markBeanAsCreated(beanName);}StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate").tag("beanName", name);try { if (requiredType != null) {beanCreation.tag("beanType", requiredType::toString); } RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. // 保证当前的bean所依赖的bean已经初始化 String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) {for (String dep : dependsOn) { if (isDependent(beanName, dep)) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } registerDependentBean(dep, beanName); try {getBean(dep); } catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"'" + beanName + "' depends on missing bean '" + dep + "'", ex); }} } // Create bean instance. // 创建bean的实例 if (mbd.isSingleton()) {sharedInstance = 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.// 从单例缓存中删除实例,它可能已经在这里// 通过创建过程-允许循环引用解析// 删除接收到任何对bean引用的临时beandestroySingleton(beanName);throw ex; }});beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) {// It's a prototype -> create a new instance.Object prototypeInstance = null;try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args);}finally { afterPrototypeCreation(beanName);}beanInstance = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } ... 省略代码...finally { beanCreation.end();} } return adaptBeanInstance(name, beanInstance, requiredType);}

此处传入进来的beanName为teacher

doGetBean()->createBean()

分开看

// Create bean instance.// 创建bean的实例if (mbd.isSingleton()) { sharedInstance = 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. // 从单例缓存中删除实例,它可能已经在这里 // 通过创建过程-允许循环引用解析 // 删除接收到任何对bean引用的临时bean destroySingleton(beanName); throw ex;} }); beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}

重要的点在这里当没有获取到bean的时候就会去调用createBean方法,创建bean,最终其实是走的doCreateBean方法去创建bean

createBean()->doCreateBean()

这里就到了上面方法线的第四部

protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException { // Instantiate the bean. // BeanWrapper:持有创建出来的Bean BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) {/** * 创建bean的实例 * 实例化但是并未初始化,就是没有给bean的属性复制 */instanceWrapper = createBeanInstance(beanName, mbd, args); } Object bean = instanceWrapper.getWrappedInstance(); Class getEarlyBeanReference(beanName, mbd, bean)); } // 到这里,bean就已经实例化完成,并且将bean放入到了singletonFactories缓存中 // Initialize the bean instance. // 初始化bean的实例Object exposedObject = bean; try {/** * 填充bean,填充Bean的属性 */populateBean(beanName, mbd, instanceWrapper);/** * 去执行 * BeanPostProcessor的postProcessBeforeInitialization方法 * */exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) {if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex;}else { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);} } if (earlySingletonExposure) {Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) { if (exposedObject == bean) {exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);Set actualDependentBeans = new LinkedHashSet(dependentBeans.length);for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean); }}if (!actualDependentBeans.isEmpty()) { throw new BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");} }} } // Register bean as disposable. // 将bean注册为一次性的 try {registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject;}

分开解释doCreateBean方法

// Instantiate the bean. // BeanWrapper:持有创建出来的Bean BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) {/** * 创建bean的实例 * 实例化但是并未初始化,就是没有给bean的属性复制 */instanceWrapper = createBeanInstance(beanName, mbd, args); }

初始化bean,这个地方开始调用createBeanInstance方法创建一个bean的实例

// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.// 缓存单例的bean能够解析循环引用// 即使生命周期接口触发像BeanFactoryAware,// 判断当前的bean是否需要提前曝光(加入singletonFactories缓存):bean是单例的&允许循环依赖&bean正在创建boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) { if (logger.isTraceEnabled()) {logger.trace("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references"); } // 将bean添加到singletonFactories也就是说的三级缓存,但是这个地方的属性是没有赋值的 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));}

记住addSingletonFactory()方法,这是循环依赖的核心

protected void addSingletonFactory(String beanName, ObjectFactory

此处存入的singletonFactory是一个lambda表达式,ObjectFactory是一个函数接口,当执行getObject没办法的时候会去调用存入的getEarlyBeanReference(beanName, mbd, bean)

doCreateBean() -> createBeanInstance()

这里也没什么好说的就是通过反射去创建Teacher对象

createBeanInstance() -> populateBean()

这里就是开始给创建的Teacher属性student赋值了

/** * Populate the bean instance in the given BeanWrapper with the property values * from the bean definition. * @param beanName the name of the bean * @param mbd the bean definition for the bean * @param bw the BeanWrapper with bean instance * 允许属性值填充给BeanWrapper中的Bean实例 */@SuppressWarnings("deprecation")// for postProcessPropertyValues后处理属性值protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {... 省略代码 ... // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the 给所有InstantiationAwareBeanPostProcessors有修改的机会// state of the bean before properties are set. This can be used, for example,// to support styles of field injection.// 设置属性之前bean的状态,例如// 支持字段注入if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}}}PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);int resolvedAutowireMode = mbd.getResolvedAutowireMode();if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {MutablePropertyValues newPvs = new MutablePropertyValues(pvs);// Add property values based on autowire by name if applicable. 通过名称自动注入参数的值if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {autowireByName(beanName, mbd, bw, newPvs);}// Add property values based on autowire by type if applicable. 通过类型注入参数的值if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {autowireByType(beanName, mbd, bw, newPvs);}pvs = newPvs;}/** * 有没有实例化的AwareBeanPostProcessor */boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();/** * 是否需要深度检查 */boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);if (hasInstAwareBpps) {if (pvs == null) {pvs = mbd.getPropertyValues();}for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);if (pvsToUse == null) {return;}pvs = pvsToUse;}}if (needsDepCheck) {PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);checkDependencies(beanName, mbd, filteredPds, pvs);}if (pvs != null) {// 应用给定的属性值,解决任何在这个bean工厂运行时它bean的引用。必须使用深copy。所以不会永久的修改此属性applyPropertyValues(beanName, mbd, bw, pvs);}}

分开解析

if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}}}

InstantiationAwareBeanPostProcessor的方法
postProcessAfterInstantiation,该方法的返回值是boolean,如果返回true,则什么都不干,如果返回false,那么此类则不会进行自动装配(属性填充),这里就是可以让我们通过postprocessor的方式控制某些bean不用属性填充。这里很明显如果我们没做特殊处理,这里最里面的if的return是不会被执行到的。

if (pvs != null) {// 应用给定的属性值,解决任何在这个bean工厂运行时它bean的引用。必须使用深copy。所以不会永久的修改此属性applyPropertyValues(beanName, mbd, bw, pvs);}

这里就是给Teacher的student属性赋值的

/** * Apply the given property values, resolving any runtime references * to other beans in this bean factory. Must use deep copy, so we * don't permanently modify this property. * @param beanName the bean name passed for better exception information * @param mbd the merged bean definition * @param bw the BeanWrapper wrapping the target object * @param pvs the new property values *应用给定的属性值,解析对此 bean 工厂中其他 bean 的任何运行时引用。必须使用深拷贝,所以我们不会永久修改这个属性 */protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) { if (pvs.isEmpty()) {// 如果pvs没有propertyValues,直接结束return; } MutablePropertyValues mpvs = null; List original; if (pvs instanceof MutablePropertyValues) {mpvs = (MutablePropertyValues) pvs;if (mpvs.isConverted()) { // Shortcut: use the pre-converted values as-is. try {bw.setPropertyValues(mpvs);return; } catch (BeansException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex); }}original = mpvs.getPropertyValueList(); } else {original = Arrays.asList(pvs.getPropertyValues()); } TypeConverter converter = getCustomTypeConverter(); if (converter == null) {converter = bw; } BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter); // Create a deep copy, resolving any references for values. 创建一个深copy,解析任何引用值 List deepCopy = new ArrayList(original.size()); boolean resolveNecessary = false; for (PropertyValue pv : original) {if (pv.isConverted()) { deepCopy.add(pv);}else { // 获取属性的名称 String propertyName = pv.getName(); // 获取属性的值 Object originalValue = pv.getValue(); if (originalValue == AutowiredPropertyMarker.INSTANCE) {Method writeMethod = bw.getPropertyDescriptor(propertyName).getWriteMethod();if (writeMethod == null) { throw new IllegalArgumentException("Autowire marker for property without write method: " + pv);}originalValue = new DependencyDescriptor(new MethodParameter(writeMethod, 0), true); } // 解析属性值 Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); Object convertedValue = resolvedValue; boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName); if (convertible) {convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter); } // Possibly store converted value in merged bean definition, // in order to avoid re-conversion for every created bean instance. if (resolvedValue == originalValue) {if (convertible) { pv.setConvertedValue(convertedValue);}deepCopy.add(pv); } else if (convertible && originalValue instanceof TypedStringValue && !((TypedStringValue) originalValue).isDynamic() && !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {pv.setConvertedValue(convertedValue);deepCopy.add(pv); } else {resolveNecessary = true;deepCopy.add(new PropertyValue(pv, convertedValue)); }} } if (mpvs != null && !resolveNecessary) {mpvs.setConverted(); } // Set our (possibly massaged) deep copy. try {bw.setPropertyValues(new MutablePropertyValues(deepCopy)); } catch (BeansException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex); }}

解析属性值

Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);

此处会直接走到resolveReference方法中去

/** * Resolve a reference to another bean in the factory.解析对另一个bean的引用 */@Nullableprivate Object resolveReference(Object argName, RuntimeBeanReference ref) { try { // 用来存放实例化出来的beanObject bean;// 获取bean的类型Class

方法会走到这里去getBean() 之前的getBean还没走完是不是有走到getBean(),从这里开始就是套娃。

resolvedName = String.valueOf(doEvaluate(ref.getBeanName()));// 获取resolvedName的bean对象bean = this.beanFactory.getBean(resolvedName); }// 注册依赖的bean

到此处就会去寻找Student的实例,就会走一遍之前的方法,但是走到pupolate()方法的时候给student的teacher属性赋值,会去容器中获取一个teacher,还记得之前存在singletonFactories中的teacher吗?这里获取的时候就会直接拿到之前的存储的teacher。下面看一看

省略之前创建个逻辑,直接到赋值的操作

到这里就开始去获取teacher对象了,看一下getSingleton()方法是怎么拿的;

protected Object getSingleton(String beanName, boolean allowEarlyReference) { // Quick check for existing instance without full singleton lock // 从单例对象缓存(singletonObjects--一级缓存)中获取bean对象 Object singletonObject = this.singletonObjects.get(beanName); // 如果单例对象中没有找到,并且改bean正在创建中 if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {// 从早期单例对象缓存中获取单例对象(之所以成为早期单例对象,是因为earlySingletonObjects里面// 的对象都是通过提前曝光的ObjectFactory创建出来的。还没有进行属性填充等操作)singletonObject = this.earlySingletonObjects.get(beanName);// 早期单例对象缓存(二级缓存)中也没有并且允许创建早期单例对象if (singletonObject == null && allowEarlyReference) { // 如果为空,则锁定全局变量进行处理 synchronized (this.singletonObjects) {// Consistent creation of early reference within full singleton lock//在完整的单例锁中一致地创建早期引用singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null) {// 当某些方法需要提前初始化的时候则会调用addSingletonFactory方法将对应的objectFactory初始化策略储存在singletonFactories中ObjectFactory

这里拿到了之前存入singletonFactoriesMap中的lambda表达式,调用getObject()方法去执行getEarlyBeanReference方法

/** * Obtain a reference for early access to the specified bean, * typically for the purpose of resolving a circular reference. * @param beanName the name of the bean (for error handling purposes) * @param mbd the merged bean definition for the bean * @param bean the raw bean instance * @return the object to expose as bean reference * * 获得对指定bean的早期访问的引用 通常用于解析循环依赖 */protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { // 默认最终公开的对象是bean,通过createBeanInstance创建出来的普通对象 Object exposedObject = bean; // mbd的synthetic属性:设置bean定义是否是synthetic的,一般是指只有AOP相关的pointCut配置或者advice配置才会将synthetic设置为true // 如果mbd不是synthetic且此工厂拥有InstantiationAwareBeanPostProcessor if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {// 遍历工厂的所有后置处理器,并获取smartInstantiationAware-ArrayListfor (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) { // 让exposedObject对象经过每一个smartInstantiationAwareBeanPostProcessor报装 exposedObject = bp.getEarlyBeanReference(exposedObject, beanName);} } // 返回最终经过层次报装后的对象 return exposedObject;}

这个方法没有什么好解释的,注释很明确的表明了方法的作用

拿到teacher之后就给Student中的teacher属性赋值

resolveNecessary = true;deepCopy.add(new PropertyValue(pv, convertedValue));

就此Student对像创建完毕,会将创建完成的Student对象放入

try { // 去容器中获取bean对象 singletonObject = singletonFactory.getObject(); newSingleton = true;}catch (IllegalStateException ex) { // Has the singleton object implicitly appeared in the meantime -> // if yes, proceed with it since the exception indicates that state. singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) {throw ex; }}catch (BeanCreationException ex) { if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) { ex.addRelatedCause(suppressedException);} } throw ex;}finally { if (recordSuppressedExceptions) {this.suppressedExceptions = null; } afterSingletonCreation(beanName);}if (newSingleton) { // 将beanName和singletonObject的映射关系添加到该工厂的单例缓存中 addSingleton(beanName, singletonObject);}

addSingleton(beanName, singletonObject);放入缓存中

至此会返回去给Teacher中的student属性赋值。至此一次的循环依赖就完成了。Spring还回去创建Student对象,但是这次容器中存在直接取出来就可以了。

疑问解答

为什么最后还要去创建一次Student对象,因为开始创建Student对象是因为创建Teacher对象的时候需要使用Student得实例,所以去创建了一次,但是最后一次去创建Student对象的时候不会真的创建,直接从缓存singletonObjects中就能去获取到。

如果文章中有错误欢迎指出,希望能对你有所帮助!

需要领取免费资料的小伙伴们,添加小助手vx:SOSOXWV 即可免费领取资料哦!