Spring的三级缓存和循环依赖 - 极悦
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Spring的三级缓存和循环依赖

Spring的三级缓存和循环依赖

更新时间:2021-06-16 12:35:23 来源:极悦 浏览873次

1. 循环依赖

什么是依赖注入?假设有两个类A和B,A在实例化的时候需要B的实例,而B在实例化时又需要A的实例,在类的实例化过程就陷入死循环。这也就是传统逻辑上的,“到底是先有鸡,还是先有蛋”的问题?

下面举一个例子,定义了两个类Type和Org:

// Org.java
@Data
@Component
public class Org {
    private final Role role;

    public Org(Role role) {
        this.role = role;
    }
}
// Role.java
@Data
@Component
public class Role {
    private final Org org;
    public Role(Org org) {
        this.org = org;
    }
}

这是spring中典型的构造器注入方式,其实也代表了普通非spring bean之间,相互依赖时的实例化过程,但结果在运行的时候直接报循环依赖的错误:

***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
   demoController (field private pers.kerry.exercise.springexercise.pojo.Org pers.kerry.exercise.springexercise.controller.DemoController.org)
┌─────┐
|  org defined in file [/Users/kerry/code/idea/spring-exercise/target/classes/pers/kerry/exercise/springexercise/pojo/Org.class]
↑     ↓
|  role defined in file [/Users/kerry/code/idea/spring-exercise/target/classes/pers/kerry/exercise/springexercise/pojo/Role.class]
└─────┘

而如果我们改一下代码,把构造器注入方式改成基于属性的注入(@Autowired、@Resouce),奇怪的是不报错了,而且相互依赖的两个bean 都实例化成功了。说明spring框架有解决循环依赖的问题,我们了解spring解决循环依赖的过程,其实有助于进一步了解spring 中 bean的活动过程。

2. 三级缓存

我们在之前介绍Bean的生命周期时说过,spring 中 bean的实例化过程,并非只是调用构造方法。除去spring框架本身提供的一些钩子或扩展方法,简单分成下面三个核心方法:

Spring在创建Bean的过程中分为三步

实例化,对应方法:AbstractAutowireCapableBeanFactory中的createBeanInstance方法,简单理解就是new了一个对象。

属性注入,对应方法:AbstractAutowireCapableBeanFactory的populateBean方法,为实例化中new出来的对象填充属性和注入依赖。

初始化,对应方法:AbstractAutowireCapableBeanFactory的initializeBean,执行aware接口中的方法,初始化方法,完成AOP代理。

从单例Bean的初始化来看,主要可能发生循环依赖的环节就在第二步populate。值得注意的是,基于构造方法注入的方式,其实是将第一步和第二步同时进行,因此马上就抛出错误。而spring通过基于属性注入的方式,是否有其他特殊的处理呢,我们这时候就要提到spring的三级缓存:

private final Map singletonObjects = new ConcurrentHashMap<>(256);

private final Map earlySingletonObjects = new HashMap<>(16);

private final Map> singletonFactories = new HashMap<>(16);

3. 核心方法:getSingleton

我们在获取bean实例的时候,其实是先从三级缓存中获取,getBean 方法的逻辑如下:

Object sharedInstance = getSingleton(beanName);
public Object getSingleton(String beanName) {
    return getSingleton(beanName, true);
}
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    // 查询缓存中是否有创建好的单例
    Object singletonObject = this.singletonObjects.get(beanName);
    // 如果缓存不存在,判断是否正在创建中
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        // 加锁防止并发
        synchronized (this.singletonObjects) {
            // 从earlySingletonObjects中查询是否有early缓存
            singletonObject = this.earlySingletonObjects.get(beanName);
            // early缓存也不存在,且允许early引用
            if (singletonObject == null && allowEarlyReference) {
                // 从单例工厂Map里查询beanName
                ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                if (singletonFactory != null) {
                    // singletonFactory存在,则调用getObject方法拿到单例对象
                    singletonObject = singletonFactory.getObject();
                    // 将单例对象添加到early缓存中
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    // 移除单例工厂中对应的singletonFactory
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }
    return (singletonObject != NULL_OBJECT ? singletonObject : null);
}

只针对单例的bean,多例的后面讨论

默认的singletonObjects缓存不存在要get的beanName时,判断beanName是否正在创建中

从early缓存earlySingletonObjects中再查询,early缓存是用来缓存已实例化但未组装完成的bean

如果early缓存也不存在,从singletonFactories中查找是否有beanName对应的ObjectFactory对象工厂

如果对象工厂存在,则调用getObject方法拿到bean对象

将bean对象加入early缓存,并移除singletonFactories的对象工厂

这是 getBean的逻辑,三级缓存中一级一级地找匹配的Bean,直到最后一级缓存,通过匹配beanName 的 ObjectFactory 来获取Bean。那么singletonFactories何时放入了可以通过getObject获得bean对象的ObjectFactory呢?

4. 核心方法:doCreateBean

Bean的实例化,实际执行的源码是AbstractAutowireCapableBeanFactory类的doCreateBean方法:

 protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {
        // 1、创建一个对bean原始对象的包装对象-BeanWrapper,执行createBeanInstance,即构造方法或工厂方法,给BeanWrapper赋值
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            instanceWrapper = this.createBeanInstance(beanName, mbd, args);
        }
        Object bean = instanceWrapper.getWrappedInstance();
        Class<?> beanType = instanceWrapper.getWrappedClass();
        if (beanType != NullBean.class) {
            mbd.resolvedTargetType = beanType;
        }
        // 2、允许其他修改beanDefinition,如使用Annotation增强Bean定义等,这通过类MergedBeanDefinitionPostProcessor来完成
        synchronized(mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
                try {
                    this.applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                } catch (Throwable var17) {
                    throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", var17);
                }

                mbd.postProcessed = true;
            }
        }
        // 3、将当前bean 的 ObjetFactory放入singletonFactories中, 
        boolean earlySingletonExposure = mbd.isSingleton() && this.allowCircularReferences && this.isSingletonCurrentlyInCreation(beanName);
        if (earlySingletonExposure) {
            if (this.logger.isTraceEnabled()) {
                this.logger.trace("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references");
            }
            this.addSingletonFactory(beanName, () -> {
                return this.getEarlyBeanReference(beanName, mbd, bean);
            });
        }
        Object exposedObject = bean;
        // 4、执行 populateBean,设置属性值
        // 5、执行 initializeBean,调用 Bean的初始化方法
        try {
            this.populateBean(beanName, mbd, instanceWrapper);
            exposedObject = this.initializeBean(beanName, exposedObject, mbd);
        } catch (Throwable var18) {
            if (var18 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var18).getBeanName())) {
                throw (BeanCreationException)var18;
            }

            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var18);
        }
        // 6、再次处理循环依赖问题
        if (earlySingletonExposure) {
            Object earlySingletonReference = this.getSingleton(beanName, false);
            if (earlySingletonReference != null) {
                if (exposedObject == bean) {
                    exposedObject = earlySingletonReference;
                } else if (!this.allowRawInjectionDespiteWrapping && this.hasDependentBean(beanName)) {
                    String[] dependentBeans = this.getDependentBeans(beanName);
                    Set<String> actualDependentBeans = new LinkedHashSet(dependentBeans.length);
                    String[] var12 = dependentBeans;
                    int var13 = dependentBeans.length;
                    for(int var14 = 0; var14 < var13; ++var14) {
                        String dependentBean = var12[var14];
                        if (!this.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.");
                    }
                }
            }
        }
        // 7、注册bean的销毁回调方法,在beanFactory中注册销毁通知,以便在容器销毁时,能够做一些后续处理工作
        try {
            this.registerDisposableBeanIfNecessary(beanName, bean, mbd);
            return exposedObject;
        } catch (BeanDefinitionValidationException var16) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", var16);
        }
    }

BeanWrapper

BeanWrapper接口,作为spring内部的一个核心接口,正如其名,它是bean的包裹类,即在内部中将会保存该bean的实例,提供其它一些扩展功能。同时,BeanWrapper接口还继承了PropertyAccessor, propertyEditorRegistry, TypeConverter、ConfigurablePropertyAccessor接口,所以它还提供了访问bean的属性值、属性编辑器注册、类型转换等功能。

我们回顾一下bean的实例化过程:

ResourceLoader加载配置信息

BeanDefinitionReader读取并解析标签,并将标签的属性转换为BeanDefinition对应的属性,并注册到BeanDefinitionRegistry注册表中。

容器扫描BeanDefinitionRegistry注册表,通过反射机制获取BeanFactoryPostProcessor类型的工厂后处理器,并用这个工厂后处理器对BeanDefinition进行加工。

根据处理过的BeanDefinition,实例化bean。然后BeanWrapper结合BeanDefinitionRegistry和PropertyEditorRegistry对Bean的属性赋值。

以上就是极悦小编介绍的"Spring的三级缓存和循环依赖",希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>