此时serviceBB开始递归调用getBean()方法,开始重复上述流程,关注流程第五步。此时bean serviceBB实例化完成,并且开始为属性赋值,发现有一个属性为serviceAA。于是尝试获取serviceAA。
进入this.beanFactory.getBean()方法。进入doGetBean()方法。后进入getSingleton(String beanName, boolean allowEarlyReference)
protected Object getSingleton(String beanName, boolean allowEarlyReference) { //一级缓存中是否存在当前bean(serviceAA),此时为null //isSingletonCurrentlyInCreation(beanName),当前bean是否被标记为 //正在创建,当前为true,进入判断 Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { //判断二级缓存中是否有bean serviceAA,当前返回null singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null && allowEarlyReference) { //通过beanname获得了之前在三级缓存中添加的创建serviceAA的工厂方法 ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { //此时singletonObject指向之前实例化完成的serviceAA对象 singletonObject = singletonFactory.getObject(); //将serviceAA添加到二级缓存中,并从三级缓存中删除 this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return (singletonObject != NULL_OBJECT ? singletonObject : null); }