设为首页 加入收藏

TOP

Spring中的循环依赖解决详解(五)
2019-09-03 03:40:17 】 浏览:243
Tags:Spring 循环 依赖 解决 详解
; synchronized (this.singletonObjects) {
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null) {
                // 省略无关代码
                beforeSingletonCreation(beanName); // 步骤A
                boolean newSingleton = false;
                // 省略无关代码
                try {
                    singletonObject = singletonFactory.getObject();// 步骤B
                    newSingleton = true;
                }
                // 省略无关代码
                finally {
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = null;
                    }
                    afterSingletonCreation(beanName);// 步骤C
                }
                if (newSingleton) {
                    addSingleton(beanName, singletonObject);// 步骤D
                }
            }
            return singletonObject;
        }
    }


获取单例对象的主要逻辑就是此方法实现的,主要分为上面四个步骤,继续挨个看:


步骤A:


1 protected void beforeSingletonCreation(String beanName) {
2        // 判断,并首次将beanName即teacher放入singletonsCurrentlyInCreation中
3        if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
4            throw new BeanCurrentlyInCreationException(beanName);
5        }
6    }


步骤C:


1 protected void afterSingletonCreation(String beanName) {
2        // 得到单例对象后,再讲beanName从singletonsCurrentlyInCreation中移除
3        if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
4            throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
5        }
6    }


步骤D:


protected void addSingleton(String beanName, Object singletonObject) {
        synchronized (this.singletonObjects) {
            this.singletonObjects.put(beanName, singletonObject);//添加单例对象到map中
            this.singletonFactories.remove(beanName);//从早期暴露的工厂中移除,此map在解决循环依赖中发挥了关键的作用
            this.earlySingletonObjects.remove(beanName);//从早期暴露的对象map中移除
            this.regi

首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python类中的魔法方法之 __slots__ 下一篇Spring源码解读之BeanFactoryPost..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目