浅挖编码中遇到的循环依赖问题 - 阿里技术

本文通过实际案例挖掘编码中遇到的循环依赖问题。

一、问题简述

// 在离线打标服务类中注入审批服务,在“申请打标”时调用 
public class PortraitOfflineLabelingServiceImpl implements PortraitOfflineLabelingService { 

    @Resource 
    private AuditService auditService; 
    /** 申请打标 */ 
    Boolean applyOdpsOfflineLabeling(Request request); 
    /** 执行打标 */ 
    Boolean executeOdpsOfflineLabeling(Request request); 
} 
// 审批服务类,定义了操作类型-审批回调服务的map映射 
@Transactional(rollbackFor = Throwable.class) 
public class AuditServiceImpl implements AuditService,ApplicationContextAware,InitializingBean { 

    private Map<EntityOperation<?>, AuditCallback> auditCallbackMap = new HashMap<>(); 
    @Override 
    public void afterPropertiesSet() throws Exception { 
        Map<EntityOperation<?>, AuditCallback> auditCallbackMap = new HashMap<>(); 
        Collection<AuditCallback> beans = applicationContext.getBeansOfType(AuditCallback.class) 
                .values(); 
        beans.forEach(auditCallback -> auditCallback.supportBizOperations() 
                .forEach(item -> auditCallbackMap.put(item, auditCallback))); 
        this.auditCallbackMap = auditCallbackMap; 
    } 
} 
// 在离线打标审批回调类中注入离线打标服务类,用于调用“执行打标”的服务 
public class OdpsOfflineLabelingCallback implements AuditCallback { 
    @Resource 
    private PortraitOfflineLabelingService offlineLabelingService; 
    @Override 
    public List<EntityOperation<?>> supportBizOperations() { 
        return Lists.newArrayList(OdpsOfflineLabelingOperation.CREATE_ODPS_OFFLINE_LABELING_INFO); 
    } 
}

形成循环依赖:

PortraitOfflineLabelingServiceImpl -> AuditServiceImpl -> AuditCallback -> PortraitOfflineLabelingServiceImpl

二、为什么会出现循环依赖报错?

2.1. Spring Bean加载过程

引用自阿里其他文章

Spring中以do开头的方法一般都是[干大事]的方法,doGetBean是用来获取bean的,doCreateBean是用来创建bean的,三个步骤包括:实例化bean->bean属性注入->初始化bean。

2.1.1. doGetBean

2.1.1.1. Spring三级缓存

  • singletonObjects: 一级缓存,保存实例化&属性注入&初始化完成的bean实例。数据结构是bean名称->bean实例的映射。
  • earlySingletonObjects: 二级缓存,用于保存实例化完成,但为属性注入和初始化完成的bean实例。数据结构是bean名称->bean实例的映射。
  • singletonFactories: 三级缓存,用于保存bean创建工厂,以便于后面扩展有机会创建代理对象并放入二级缓存。数据结构是bean名称->bean创建工厂的映射。

    2.1.1.2. 通过三级缓存获取bean实例

protected Object getSingleton(String beanName, boolean allowEarlyReference) { 
    // 尝试从一级缓存中获取已经初始化完成的bean实例(完全装载好的bean) 
    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(); 
                            // 将获取的bean从三级缓存中移除,并且升级到二级缓存中 
                            this.earlySingletonObjects.put(beanName, singletonObject); 
                            this.singletonFactories.remove(beanName); 
                        } 
                    } 
                } 
            } 
        } 
    } 
    // 返回获取的单例bean 
    return singletonObject; 
}

2.1.2. doCreateBean

Bean的加载最核心的代码就在doCreateBean方法中,包括三个阶段:

  • createBeanInstance: 实例化Bean,获得未被填充属性的原始Bean。
  • populateBean: 如果Bean有需要注入的属性,则进行属性填充,前提是需要填充的属性已经存在于Spring容器中,否则会先加载该属性再进行填充。如果有循环依赖,问题就是在这个过程中发生的。
  • initializeBean: 执行bean的初始化过程,包括执行前置方法->执行初始化->执行后置方法。
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) 
      throws BeanCreationException { 
    // 1.实例化bean 
    // 封装被创建的Bean对象 
    BeanWrapper instanceWrapper = null; 
    if (mbd.isSingleton()) { 
        instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); 
    } 
    if (instanceWrapper == null) { 
        instanceWrapper = createBeanInstance(beanName, mbd, args); 
    } 
    // 获取实例化对象的类型 
    Object bean = instanceWrapper.getWrappedInstance(); 
    Class<?> beanType = instanceWrapper.getWrappedClass(); 
    if (beanType != NullBean.class) { 
        mbd.resolvedTargetType = beanType; 
    } 
    // 调用PostProcessor后置处理器对bean进行一些操作 
    synchronized (mbd.postProcessingLock) { 
        if (!mbd.postProcessed) { 
            try { 
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); 
            } 
            catch (Throwable ex) { 
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, 
                        "Post-processing of merged bean definition failed", ex); 
            } 
            mbd.postProcessed = true; 
        } 
    } 
    // 当允许提前暴露时,将实例化好的bean放进singletonFactories三级缓存,用来解决循环依赖导致的问题。 
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && 
            isSingletonCurrentlyInCreation(beanName)); 
    if (earlySingletonExposure) { 
        if (logger.isTraceEnabled()) { 
            logger.trace("Eagerly caching bean '" + beanName + 
                    "' to allow for resolving potential circular references"); 
        } 
        addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); 
    } 
    Object exposedObject = bean; 
    try { 
        // 2.开始填充bean属性(依赖注入) 
        populateBean(beanName, mbd, instanceWrapper); 
        // 3.执行初始化方法(包括前后置的处理器) 
        exposedObject = initializeBean(beanName, exposedObject, mbd); 
    } 
    catch (Throwable ex) { 
        if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { 
            throw (BeanCreationException) ex; 
        } 
        else { 
            throw new BeanCreationException( 
                    mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); 
        } 
    } 
    // 当通过提早暴露解决循环依赖问题时,需要进行单例校验;这里也是本次报错的地方,后面会讲到为啥报错。 
    if (earlySingletonExposure) { 
        Object earlySingletonReference = getSingleton(beanName, false); 
        if (earlySingletonReference != null) { 
            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); 
                    } 
                } 
                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 " + 
                            "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example."); 
                } 
            } 
        } 
    } 
    try { 
        registerDisposableBeanIfNecessary(beanName, bean, mbd); 
    } 
    catch (BeanDefinitionValidationException ex) { 
        throw new BeanCreationException( 
                mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); 
    } 
    return exposedObject; 

}

2.2. 循环依赖情况下bean的加载过程

案例:Bean A和Bean B互相依赖对方。

2.3. Spring三级缓存没有解决本次报错的原因

前提:Spring管理的Bean默认都是单例的,所以Spring默认需要保证所有使用此Bean的地方都指向的是同一个地址,也就是最终版本的Bean,否则可能就乱套了。Bean在初始化完成后,也提供了单例校验的逻辑。

结论:AOP代理导致单例校验失败抛错。

initializeBean方法对传入的bean进行了初始化处理,当被AOP代理时会导致返回的bean和传入的bean不是同一个bean对象。AuditServiceImpl类上加了@Transactional注解,导致bean初始化的时候会被AOP代理,填充的属性为代理而非本体,从而导致单例校验抛错。

尝试去除AuditServiceImpl类上的@Transactional注解后,能够部署成功:

三、怎么解决本次报错

3.1. 从根源解决-去除循环依赖

// 拆分申请打标和执行打标到两个服务中,打破循环依赖 
public class PortraitApplyOfflineLabelingServiceImpl implements PortraitApplyOfflineLabelingService { 

    @Resource 
    private AuditService auditService; 
    /** 申请打标 */ 
    Boolean applyOdpsOfflineLabeling(Request request); 
} 
public class PortraitExecuteOfflineLabelingServiceImpl implements PortraitExecuteOfflineLabelingService { 
    /** 执行打标 */ 
    Boolean executeOdpsOfflineLabeling(Request request); 
}

3.2. @Lazy

public class OdpsOfflineLabelingCallback implements AuditCallback { 
    // 在属性注入的时候增加懒加载的注解 
    @Lazy 
    @Resource 
    private PortraitOfflineLabelingService offlineLabelingService; 
    @Override 
    public List<EntityOperation<?>> supportBizOperations() { 
        return Lists.newArrayList(OdpsOfflineLabelingOperation.CREATE_ODPS_OFFLINE_LABELING_INFO); 
    } 
}
  • 创建AuditServiceImpl并填充属性PortraitOfflineLabelingServiceImpl的时候,发现是@Lazy懒注入,则生成一个代理对象直接赋值了,不会再执行去缓存中寻找PortraitOfflineLabelingServiceImpl、找不到再创建的步骤了,而是AuditServiceImpl直接正常走完后续生命周期流程,最终放入单例池。
  • 而到单例PortraitOfflineLabelingServiceImpl创建的时候,填充属性AuditServiceImpl时直接能从单例池拿到完整的bean,因此PortraitOfflineLabelingServiceImpl也能正常走完后续生命周期流程。
  • 最后,当AuditServiceImpl真正用到懒加载的属性,执行其方法的时候,才会去单例池中寻找真正的bean。
8