谈一下经典的面试题 如何解决IOC的循环依赖

问题描述:

    比如两个Bean   InstacneA  和InstacneB 里面存在属性相互引用 创建Bean的时候 会相互依赖。

1.构造器注入 直接失败

2.protolType为多例的时候 也失败

3.单例 且是setter注入能成功创建

透过表面现象 看本质问题

Spring容器如何解决的呢?

源码走起~

思路 容器启动的时候便是 refresh()方法中  最后实例Bean的时候 会调用到

org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean


@SuppressWarnings("unchecked")

protected T doGetBean(

final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly)

throws BeansException {

//解析bean 别名 alias

final String beanName = transformedBeanName(name);

  Object bean;

  // Eagerly check singleton cache for manually registered singletons.

  //尝试从 单例子缓存池子内 去取   第一次创建的时候没有实例

  Object sharedInstance = getSingleton(beanName);

  if (sharedInstance !=null && args ==null) {

if (logger.isDebugEnabled()) {

if (isSingletonCurrentlyInCreation(beanName)) {

logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +

"' that is not fully initialized yet - a consequence of a circular reference");

        }

else {

logger.debug("Returning cached instance of singleton bean '" + beanName +"'");

        }

}

bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);

  }

else {

//拿不到当前单例 就往下走 

// Fail if we're already creating this bean instance:

// We're assumably within a circular reference.

//多例的则抛出异常

      if (isPrototypeCurrentlyInCreation(beanName)) {

throw new BeanCurrentlyInCreationException(beanName);

      }

// Check if bean definition exists in this factory.

      BeanFactory parentBeanFactory = getParentBeanFactory();

      if (parentBeanFactory !=null && !containsBeanDefinition(beanName)) {

// Not found -> check parent.

        String nameToLookup = originalBeanName(name);

        if (args !=null) {

// Delegation to parent with explicit args.

            return (T) parentBeanFactory.getBean(nameToLookup, args);

        }

else {

// No args -> delegate to standard getBean method.

            return parentBeanFactory.getBean(nameToLookup, requiredType);

        }

}

if (!typeCheckOnly) {

markBeanAsCreated(beanName);

      }

try {

// 合并父类的属性到当前实例

final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

//检查当前bean 是不是抽象类 如果是抽象的则会抛出异常 所以抽象的类不会实例化

        checkMergedBeanDefinition(mbd, beanName, args);

        // Guarantee initialization of beans that the current bean depends on.

    // @DependsOn   判断实例先后顺序  加载顺序 依赖检查

        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);

              }

}

}

//创建Bean的过程开始

// Create bean instance.

        if (mbd.isSingleton()) {

sharedInstance = getSingleton(beanName, new ObjectFactory() {

@Override

              public ObjectgetObject()throws BeansException {

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.

                    destroySingleton(beanName);

                    throw ex;

                  }

}

});

            bean = 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);

            }

bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);

        }

else {

String scopeName = mbd.getScope();

            final Scope scope =this.scopes.get(scopeName);

            if (scope ==null) {

throw new IllegalStateException("No Scope registered for scope name '" + scopeName +"'");

            }

try {

Object scopedInstance = scope.get(beanName, new ObjectFactory() {

@Override

                  public ObjectgetObject()throws BeansException {

beforePrototypeCreation(beanName);

                    try {

return createBean(beanName, mbd, args);

                    }

finally {

afterPrototypeCreation(beanName);

                    }

}

});

              bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);

            }

catch (IllegalStateException ex) {

throw new BeanCreationException(beanName,

                    "Scope '" + scopeName +"' is not active for the current thread; consider " +

"defining a scoped proxy for this bean if you intend to refer to it from a singleton",

                    ex);

            }

}

}

catch (BeansException ex) {

cleanupAfterBeanCreationFailure(beanName);

        throw ex;

      }

}

// Check if required type matches the type of the actual bean instance.

  if (requiredType !=null && bean !=null && !requiredType.isInstance(bean)) {

try {

return getTypeConverter().convertIfNecessary(bean, requiredType);

      }

catch (TypeMismatchException ex) {

if (logger.isDebugEnabled()) {

logger.debug("Failed to convert bean '" + name +"' to required type '" +

ClassUtils.getQualifiedName(requiredType) +"'", ex);

        }

throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());

      }

}

return (T) bean;

}


--------------------------获取单例bean  


/**

* Return the (raw) singleton object registered under the given name.

* <p>Checks already instantiated singletons and also allows for an early

* reference to a currently created singleton (resolving a circular reference).

* @param beanName the name of the bean to look for

* @param allowEarlyReference whether early references should be created or not

* @return the registered singleton object, or {@code null} if none found

*/

protected ObjectgetSingleton(String beanName, boolean allowEarlyReference) {

Object singletonObject =this.singletonObjects.get(beanName);

// 是不是在创建这个bean

  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 !=NULL_OBJECT ? singletonObject :null);

}


//属性未被赋值的时候   早期对象A被创建

populateBean方法 发布bean的过程中去进行属性赋值

对早期对象A的属性赋值 也就是intanceB属性赋值

发现属性b 需要创建 

-------创建B

再次 调用同样对流程 创建B的早期对象(无属性) 发现依赖A实例

---->这个时候再去创建A(去早期对象缓存中拿  拿到了A的早期对象 )

B中的 A就被赋值了 

放入到一级缓存 

移除二级 三级中 缓存中的B 实例

这个时候B就被创建好了 

然后再给A赋值

为什么构造器注入会抛出异常呢?

  暴露的是一个早期对象 调用构造器 A的时候 就会去直接创建B-》....

因为你没有创建完 便没办法暴露....

但是这个是 缓存没有A 那么  

这个时候便 无法解决

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容