所谓的循环依赖是指,A 依赖 B,B 又依赖 A,它们之间形成了循环依赖。
Spring解决循环依赖Spring通过三级缓存的方式来解决循环依赖的问题。DefaultSingletonBeanRegistry类中的三个缓存变量
private final Map部分源码 获取Bean对象singletonObjects = new ConcurrentHashMap<>(256); private final Map > singletonFactories = new HashMap<>(16); private final Map earlySingletonObjects = new HashMap<>(16);
Spring通过AbstractBeanFactory#doGetBean来获取Bean对象
protected从缓存获取T doGetBean(final String name, @Nullable final Class requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { ... //尝试去缓存中获取对象:一级>二级>三级 Object sharedInstance = getSingleton(beanName); ... //创建单例bean if (mbd.isSingleton()) { //此处getSingleton不同于上面的getSingleton方法: //前面的getSingleton主要是从缓存中获取对象,如果有直接返回; //后面的getSingleton是把Bean标记成正在创建,作为循环依赖的出口 sharedInstance = getSingleton(beanName, () -> { try { //进入创建bean的逻辑 return createBean(beanName, mbd, args); } ... return (T) bean; }
通过AbstractBeanFactory#getSingleton方法,最终会调用DefaultSingletonBeanRegistry#getSingleton
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//先从一级缓存中获取
//第一次进来的时候还获取不到
Object singletonObject = this.singletonObjects.get(beanName);
//一级缓存中没有获取到并且该bean处于正在创建阶段
//第一次进该方法也不是处于正在创建阶段,所以不会走该分支
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
//尝试去二级缓存中获取对象(二级缓存中的对象是一个早期对象)
//早期对象:就是bean刚刚调用了构造方法,还来不及给bean的属性进行赋值的对象
singletonObject = this.earlySingletonObjects.get(beanName);
//二级缓存中也没有获取到对象,且allowEarlyReference为true
if (singletonObject == null && allowEarlyReference) {
//直接从三级缓存中获取ObjectFactory对象
//在ioc后期的过程中,当bean调用了构造方法的时候,把早期对象包裹成一个ObjectFactory暴露到三级缓存中
ObjectFactory> singletonFactory = this.singletonFactories.get(beanName);
//从三级缓存中获取到对象
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//把早期对象放在二级缓存
this.earlySingletonObjects.put(beanName, singletonObject);
//ObjectFactory包装对象从三级缓存中删除掉
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
标记Bean正在创建
因为第一次去缓存中获取不到对象,所以还会走下面的createBean–>doCreateBean逻辑。
在createBean之前通过另一个getSingleton方法把Bean标记成正在创建,作为循环依赖的出口:
//DefaultSingletonBeanRegistry
public Object getSingleton(String beanName, ObjectFactory> singletonFactory) {
...
//标记当前的bean正在被创建
beforeSingletonCreation(beanName);
...
// 通过钩子方法回去调用createBean()方法
singletonObject = singletonFactory.getObject();
...
//加入缓存中
addSingleton(beanName, singletonObject);
}
protected void beforeSingletonCreation(String beanName) {
//标记当前的bean正在被创建
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
//加入到单例缓存池中
this.singletonObjects.put(beanName, singletonObject);
//从三级缓存中移除(针对的不是处理循环依赖的)
this.singletonFactories.remove(beanName);
//从二级缓存中移除(循环依赖的时候 早期对象存在于二级缓存)
this.earlySingletonObjects.remove(beanName);
//用来记录保存已经处理的bean
this.registeredSingletons.add(beanName);
}
}
创建Bean
AbstractAutowireCapableBeanFactory#doCreateBean
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
...
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
//上述条件满足,允许中期暴露对象
if (earlySingletonExposure) {
...
//把早期对象包装成一个singletonFactory对象,该对象提供了一个getObject方法,该方法内部调用getEarlyBeanReference方法
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
...
//属性赋值,解析属性时发现循环依赖,会再次调用getBean方法
populateBean(beanName, mbd, instanceWrapper);
//对象初始化
exposedObject = initializeBean(beanName, exposedObject, mbd);
...
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
...
}
加到三级缓存
DefaultSingletonBeanRegistry#addSingletonFactory
用于把早期对象包装成ObjectFactory暴露到三级缓存singletonFactories中,解决循环依赖
protected void addSingletonFactory(String beanName, ObjectFactory> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
//同步加锁
synchronized (this.singletonObjects) {
//单例缓存池中没有包含当前的bean
if (!this.singletonObjects.containsKey(beanName)) {
//加入到三级缓存中,暴露早期对象用于解决循环依赖
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
流程梳理
假如存在两个类互相依赖,A 依赖 B,B 又依赖 A,形成循环依赖。
当通过getBean获取A时,先getSingleton从缓存中获取不到,所以通过createBean–>doCreateBean逻辑去进行创建,创建之前通过beforeSingletonCreation标记A正在创建;在doCreateBean中,如果符合条件earlySingletonExposure就把早期对象包装成ObjectFactory暴露到三级缓存singletonFactories中;随后进行属性赋值populateBean,在解析属性时发现依赖B,所以通过getBean去获取B;
通过getBean去获取B和获取A逻辑一样,直到解析属性时发现依赖A,所以再次通过getBean获取A;
再次获取A时,通过getSingleton去缓存中获取,此时一级缓存中还没有A,但是A已经被标记为正在创建,所以会进入if去找二级缓存;二级缓存没有去找三级缓存,从三级缓存中获取到ObjectFactory包装对象,然后获取到早期对象;把早期对象放到二级缓存并删除三级缓存中对象;
第二次获取A完成之后,返回到创建B的操作,将获取到的A赋值到B的a属性,直到创建B完成之后,返回到第一次创建A的操作,将B赋值到A的b属性,直至创建A的操作完成,整个循环依赖处理结束。继续往下走,再次调用getSingleton从二级缓存中获取到A返回。
其他情况1.构造函数的循环依赖:无法解决。因为构造函数在实例化过程中,此时还没有实例,所以无法解决循环依赖。
2.多例下的循环依赖:无法解决。
多例对象是不会放入缓存中的,暴露早期对象时会判断是否是单例,如果不是单例不会放到三级缓存中。
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
如果是多例并且正在创建会直接抛异常。
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}



