Spring循环依赖

基本概念:

  两个或两个以上的类相互引用、互相依赖;

Java使用构造器不能解决循环依赖问题;

Java可以使用setter解决循环依赖问题;

 

Spring中:

 

内部通过三级缓存来解决循环依赖问题(DefaultSingletonBeanRegistry)

只有单例的 bean 会通过三级缓存提前暴露来解决循环依赖问题,而非单例的 bean 每次从容器获取的都是一个新对象,都会重新创建,所以非单例的 bean是没有缓存的,不会放到三级缓存中;

一级缓存(也叫单例池)singletonObjects:存放已经经历了完整生命周期的 Bean对象;

二级缓存 earlySingletonObjects:存放早期暴露出来的 Bean对象,Bean的生命周期未结束(属性还未填充完整的);

三级缓存 Map<String, ObjectFactory<?>> singletonFactories:存放可以生成 Bean的工厂;

 

 

spring循环依赖总结:

    @Nullable
    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName)) {
            //一级缓存没有的话,就去二级缓存中找
            singletonObject = this.earlySingletonObjects.get(beanName);
            if (singletonObject == null && allowEarlyReference) {
                synchronized (this.singletonObjects) {
                    singletonObject = this.singletonObjects.get(beanName);
                    if (singletonObject == null) {
                        singletonObject = this.earlySingletonObjects.get(beanName);
                        if (singletonObject == null) {
                            //二级缓存没有的话就去三级缓存中找
                            ObjectFactory<?> singletonFactory = (ObjectFactory) this.singletonFactories.get(beanName);
                            if (singletonFactory != null) {
                                //三级缓存中存在的话
                                singletonObject = singletonFactory.getObject();
                                //就把它移动到二级缓存中
                                this.earlySingletonObjects.put(beanName, singletonObject);
                                //三级缓存中的移除
                                this.singletonFactories.remove(beanName);
                            }
                        }
                    }
                }
            }
        }
        return singletonObject;
    }

 

posted @ 2021-04-20 11:09  DHaiLin  阅读(67)  评论(0编辑  收藏  举报