Spring如何处理循环依赖问题?

21 篇文章 1 订阅
12 篇文章 1 订阅

Spring使用了三级缓存来解决循环依赖的问题。

三级缓存:Spring在启动的过程中,使用到了三个map,称作三级缓存。

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

	/** Maximum number of suppressed exceptions to preserve. */
	private static final int SUPPRESSED_EXCEPTIONS_LIMIT = 100;


	/** Cache of singleton objects: bean name to bean instance. */
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

	/** Cache of singleton factories: bean name to ObjectFactory. */
	private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);

	/** Cache of early singleton objects: bean name to bean instance. */
	private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);

    ...
    ...
}

实现原理:(A->B->A)

  1. 先加载A,依次判断(一级缓存)、(二级缓存)、(三级缓存)中是否有A,没有就将A加入(三级缓存)。

  2. A依赖B,先加载B。

       1.  依次判断(一级缓存)、(二级缓存)、(三级缓存)中是否有B,没有就将B加入(三级缓存)
       2. 加载B的依赖,发现依赖A,依次从(一级缓存)、(二级缓存)、(三级缓存)中查找A,发现(三级缓存)有A,将A上升到(二级缓存)中
       3. 将A注入B的引用,完成B的加载,将B从(三级缓存)升级至(一级缓存)中
    
  3. A依赖的B加载完了,继续加载A完成。将A从(二级缓存)上升到(一级缓存)。

 关键源码在*AbstractAutowireCapableBeanFactorydo.CreateBean*中,就先将bean放入singletonFactories(三级缓存)

 ```java
 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
 	throws BeanCreationException {
 .....
 
 //提前缓存单例,以便即使在诸如BeanFactoryAware之类的生命周期接口触发时也能够解析循环引用。
 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");
     }
     addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
 }
 ```

 核心判断代码

 ```java
 public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
 
 /**(一级缓存)已经完成加载的单例缓存. */
 private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
 
 /**(二级缓存)早期的单例对象缓存集合. */
 private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
 
 /**(三级缓存)单例的工厂Bean缓存集合. */ 
 private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
 ......
 
 //以此从singletonObjects(一级缓存中)、earlySingletonObjects(二级缓存)、singletonFactories(三级缓存)中查找
 protected Object getSingleton(String beanName, boolean allowEarlyReference) {
     //一级缓存获取
     Object singletonObject = this.singletonObjects.get(beanName);
     if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
         synchronized (this.singletonObjects) {
             //二级缓存获取
             singletonObject = this.earlySingletonObjects.get(beanName);
             if (singletonObject == null && allowEarlyReference) {
                 //三级缓存获取
                 ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                 if (singletonFactory != null) {
                     singletonObject = singletonFactory.getObject();
                     //升级至二级缓存
                     this.earlySingletonObjects.put(beanName, singletonObject);
                     this.singletonFactories.remove(beanName);
                 }
             }
         }
     }
     return singletonObject;
 }
 ```
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值