spring 三级缓存解决循环依赖

循环依赖

@Component
public class A {
    @Autowired
    private B b;

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }
}
@Component
public class B {
    @Autowired
    private A a;

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }
}

在创建上述两个 bean 时,比如先创建 a,会有如下步骤发生:

  1. a 实例化完成,初始化时发现要创建 b
  2. b 实例化完成,初始化时发现要创建 a,此时第1步创建的 a 并未初始化完成,所以此时不能给 bean b 的属性 a 赋值,尝试创建 a
  3. 重复第1~2步,产生循环问题

从上面的步骤可以看出来,循环依赖的核心问题在于完成实例化但未初始化的对象a不能被拿来使用(给对象b的属性a赋值),所以需要重新去创建对象a,从而产生循环问题。所以如果能提前将完成实例化但未初始化的半成品对象提前暴露出去,则可以解决循环依赖问题。

三级缓存

spring 中的三级缓存指的是三个 map

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    /** Cache of singleton objects: bean name --> bean instance */
    private final Map singletonObjects = new ConcurrentHashMap(256);

    /** Cache of singleton factories: bean name --> ObjectFactory */
    private final Map> singletonFactories = new HashMap>(16);

    /** Cache of early singleton objects: bean name --> bean instance */
    private final Map earlySingletonObjects = new HashMap(16);
}

public interface ObjectFactory {
    T getObject() throws BeansException;
}
  • 一级缓存: singletonObjects 存放实例化、初始化都完成的 bean 对象
  • 二级缓存: earlySingletonObjects 存放实例化完成但未初始化的 bean 对象
  • 三级缓存: singletonFactories 存放 ObjectFactory 对象

第一级缓存用来存放实例化、初始化完成的单例对象;
第二级缓存提前暴露对象,解决循环依赖问题;
那么为什么需要第三级缓存呢?
第三级缓存存放的是一个对象工厂,是一个 lambda 表达式,即只有在 ObjectFactory 对象调用 getObject方法时,才会去执行对应的逻辑查找/创建 bean,也就是说缓存一个对象工厂,在需要 bean 对象时,调用 getObject 方法,这是 lambda 表达式的优点。
这似乎还是没有解释第三级缓存的意义,我们为何不是直接把实例化好的 bean 对象放在二级缓存中?而是将 ObjectFactory 对象存在三级缓存中。试想一下,在一个 spring 容器里,同名的单例 bean 对象只可能存在一个,对于 aop 场景中,我们不能返回原始 bean 对象,而是需要返回对应的代理对象,那么我们在创建 bean a 的时候是不知道应该提前暴露原始 bean 对象 a 还是应该暴露 a 对应的代理对象,只有当使用 bean a 的时候,例如给其他 bean b 的属性 a 赋值时才知道这件事,也就是属性赋值时才需要去查找/创建 a 或者 a 的代理对象,这就是第三级缓存的意义。
小结:提前暴露对象可以解决循环依赖问题,第三级缓存是为了解决 aop 场景的循环依赖问题。第一级和第二级缓存理论上是可以合并的,但是弊大于利,会让编码变的复杂。第三级缓存在没有 aop 的场景下理论上可以移除。

源码中 bean 的创建过程

以下源码部分不重要的内容已被省略
getBean → doGetBean → createBean → doCreateBean → createInstance → populateBean
① org.springframework.beans.factory.support.AbstractBeanFactory#getBean

    @Override
    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null, null, false);
    }

② 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 的别名获取 bean 的原始名称
    final String beanName = transformedBeanName(name);
    Object bean;

    // Eagerly check singleton cache for manually registered singletons.
    // 从三级缓存中逐级查找 bean
    Object sharedInstance = getSingleton(beanName);
    if (sharedInstance != null && args == null) {
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    else {

        try {
            final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            checkMergedBeanDefinition(mbd, beanName, args);

            // Create bean instance.
            if (mbd.isSingleton()) {
                // 重点在于此方法 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory)
                sharedInstance = getSingleton(beanName, new ObjectFactory() {
                    @Override
                    public Object getObject() throws BeansException {
                        try {
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            destroySingleton(beanName);
                            throw ex;
                        }
                    }
                });
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            else if (mbd.isPrototype()) {
                // ......
            }

            else {
                // ......
            }
        }
        catch (BeansException ex) {
            // ......
        }
    }

    return (T) bean;
}

public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
    synchronized (this.singletonObjects) {
        // 从一级缓存中查找 bean
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {

            boolean newSingleton = false;

            try {
                // 调用入参 singletonFactory 的 getObject 方法,从而创建bean
                singletonObject = singletonFactory.getObject();
                newSingleton = true;
            }
            catch (IllegalStateException ex) {
                // ......
            }
            catch (BeanCreationException ex) {
                // ......
            }
            finally {
                // ......
            }
            if (newSingleton) {
                // 将实例化、初始化完成的 bean 放入一级缓存,并从二级、三级缓存中清掉
                addSingleton(beanName, singletonObject);
            }
        }
        return (singletonObject != NULL_OBJECT ? singletonObject : null);
    }
}
 
  

③ org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean

@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {

    RootBeanDefinition mbdToUse = mbd;

    // 创建 bean
    Object beanInstance = doCreateBean(beanName, mbdToUse, args);
    return beanInstance;
}

④ org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
        throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
        instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {
        // 使用反射创建 bean 对象,并将 bean 对象封装在 BeanWrapper 中
        instanceWrapper = createBeanInstance(beanName, mbd, args);
    }
    final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
    Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
    mbd.resolvedTargetType = beanType;

    // Eagerly cache singletons to be able to resolve circular references
    // even when triggered by lifecycle interfaces like BeanFactoryAware.
    // 是否需要提前暴露对象
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
            isSingletonCurrentlyInCreation(beanName));
    if (earlySingletonExposure) {
        // 将实例化完成但未初始化的 bean 对象对应的 ObjeactFactory 对象放入三级缓存
        addSingletonFactory(beanName, new ObjectFactory() {
            @Override
            public Object getObject() throws BeansException {
                return getEarlyBeanReference(beanName, mbd, bean);
            }
        });
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {
        // 给 bean 对象的属性赋值
        populateBean(beanName, mbd, instanceWrapper);
        if (exposedObject != null) {
            // 执行 bean 对应的 init 方法
            exposedObject = initializeBean(beanName, exposedObject, mbd);
        }
    }
    catch (Throwable ex) {
        // ......
    }

    if (earlySingletonExposure) {
        Object earlySingletonReference = getSingleton(beanName, false);
        if (earlySingletonReference != null) {
            if (exposedObject == bean) {
                exposedObject = earlySingletonReference;
            }
            else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                // ......
            }
        }
    }

    return exposedObject;
}
 
  

⑤ org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
    // No special handling: simply use no-arg constructor.
    // 无特殊处理,使用无参构造器
    return instantiateBean(beanName, mbd);
}

⑥ org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean

protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
    // 从 bean 的定义信息获取属性值
    PropertyValues pvs = mbd.getPropertyValues();

    // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
    // state of the bean before properties are set. This can be used, for example,
    // to support styles of field injection.
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                    return;
                }
            }
        }
    }

    int resolvedAutowireMode = mbd.getResolvedAutowireMode();
    if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
        MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
        // Add property values based on autowire by name if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
            autowireByName(beanName, mbd, bw, newPvs);
        }
        // Add property values based on autowire by type if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
            autowireByType(beanName, mbd, bw, newPvs);
        }
        pvs = newPvs;
    }

    boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
    boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

    if (hasInstAwareBpps || needsDepCheck) {
        PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
        if (hasInstAwareBpps) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    // 找到 AutowiredAnnotationBeanPostProcessor 为 bean 对象的属性赋值(因为代码中,我是通过 @Autowired 去注入属性值的)
                    pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                    if (pvs == null) {
                        return;
                    }
                }
            }
        }
        if (needsDepCheck) {
            checkDependencies(beanName, mbd, filteredPds, pvs);
        }
    }

    applyPropertyValues(beanName, mbd, bw, pvs);
}

总结

看 spring 源码是比较需要耐心的,并且经常容易把自己看迷糊,看完这部分源码之后,对整个创建 bean 的流程更清晰,收获也不少。

你可能感兴趣的:(spring,java,spring,boot)