Spring源码分析spring源码

Spring源码(六)-循环依赖

2017-08-22  本文已影响168人  阿亮私语

前言

结束了两天的河北游,终于回到帝都,这周事情比较多,然而还得继续把上周欠下的债给补上,这一节咱们主要分析一下Spring-IOC中之前被忽略的那些细节以及一些常见的Spring-IOC的面试题。

1、Spring循环依赖

Bean在创建的时候可以给该Bean做校验,如果递归调用回来发现正在创建中的话,即说明了循环依赖。其实这点和Spring初始化的时候读配置文件涉及到import关键字会导致循环导入时的处理手法是一致的。

【DefaultSingletonBeanRegistry】
protected void beforeSingletonCreation(String beanName) {
        if (!this.singletonsCurrentlyInCreation.add(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }
    }

上图是个单例Bean创建的实例,在创建之前先打标,然后在实例化的时候如果发现已经在创建了,就抛出异常。

【AbstractBeanFactory】
if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
    }
if (isPrototypeCurrentlyInCreation(beanName)) {
         throw new BeanCurrentlyInCreationException(beanName);
  }

//如果不是做类型检查则是创建bean,这里要进行记录
if (!typeCheckOnly) {
    markBeanAsCreated(beanName);
}
//是否需要提前曝光:单例&允许循环依赖&当bean正在创建中,检测循环依赖
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
        isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
    if (logger.isDebugEnabled()) {
        logger.debug("Eagerly caching bean '" + beanName +
                "' to allow for resolving potential circular references");
    }
    //为避免后期循环依赖,可以在bean初始化完成前将创建实例的ObjectFactory加入工厂
    addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}

为什么不把Bean暴露出去,而是暴露个Factory呢?因为有些Bean是需要被代理的,看下getEarlyBeanReference的实现:

protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
        Object exposedObject = bean;
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            for (Iterator it = getBeanPostProcessors().iterator(); it.hasNext(); ) {
                BeanPostProcessor bp = (BeanPostProcessor) it.next();
                if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                    SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                    exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
                }
            }
        }
        return exposedObject;
    }

当你依赖到了该Bean而单例缓存里面有没有该Bean的时候就会调用该工厂方法生产Bean,看下getSingleton的实现:

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {
            synchronized (this.singletonObjects) {
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
                    ObjectFactory singletonFactory = (ObjectFactory) 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);
    }

【AbstractAutoProxyCreator】
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        this.earlyProxyReferences.add(cacheKey);
        return wrapIfNecessary(bean, beanName, cacheKey);
    }
【AbstractAutoProxyCreator】
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (!this.earlyProxyReferences.contains(cacheKey)) {
            return wrapIfNecessary(bean, beanName, cacheKey);
        }
        return bean;
    }

这个BeanPostProcessor很典型了,用于创建代理类的。一般这种BeanPostProcessor总要提供一个getEarlyBeanReference的接口供其他Bean使用,而又防止了其他类直接使用到该类最原始的版本。这就是上述两个方法如此相似的原因。置于略微的差异,你应该看出来,是防止重复执行方法。

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            invokeAwareMethods(beanName, bean);
            return null;
        }, getAccessControlContext());
    }
    else {
        invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    if (wrappedBean != null) {
        try {
            invokeInitMethods(beanName, wrappedBean, mbd);
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    (mbd != null ? mbd.getResourceDescription() : null),
                    beanName, "Invocation of init method failed", ex);
        }
        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }
    }

    return wrappedBean;
}

你不能保证这些乱七八糟的BeanPostProcessor会不会改变Bean的版本,当然,如果改变了,肯定要出错的,在这里,Spring就没有做依赖解决了,只是做了依赖检查

Object earlySingletonReference = getSingleton(beanName, false);
//只有在检测到有循环依赖的情况下才会不为空
if (earlySingletonReference != null) {
    //如果exposedObject没有在初始化方法中被改变,也就是没有被增强
    if (exposedObject == bean) {
        exposedObject = earlySingletonReference;
    }
    else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
        String[] dependentBeans = getDependentBeans(beanName);
        Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
        //检测依赖
        for (String dependentBean : dependentBeans) {
            if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                actualDependentBeans.add(dependentBean);
            }
        }
        /**
         * 因为bean创建后其锁依赖的bean一定是已经创建的
         * actualDependentBeans不为空则表示当前bean创建后其依赖的bean却没有全部创建完,也就是说存在循环依赖
         */
        if (!actualDependentBeans.isEmpty()) {
            throw new BeanCurrentlyInCreationException(beanName,
                    "Bean with name '" + beanName + "' has been injected into other beans [" +
                    StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                    "] in its raw version as part of a circular reference, but has eventually been " +
                    "wrapped. This means that said other beans do not use the final version of the " +
                    "bean. This is often the result of over-eager type matching - consider using " +
                    "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
        }
    }
}

写在最后

参考 事例

<iframe frameborder="no" border="0" marginwidth="0" marginheight="20" width=198 height=52 src="https://music.163.com/outchain/player?type=2&id=32272267&auto=1&height=32"></iframe>

上一篇下一篇

猜你喜欢

热点阅读