1 概述
在前面介绍三种不同的依赖注入类型时,引出了使用Spring IoC容器时一个常见问题,即循环依赖。同时也明确了在单例作用域下,Setter方法注入能够解决循环依赖问题,而构造器注入则不能。对于单例作用域来说,Spring容器在整个生命周期内,有且只有一个Bean对象,所以很容易想到这个对象应该存在于缓存中。Spring为了解决单例Bean的循环依赖问题,使用了三级缓存。这是Spring在设计和实现上的一大特色。
2 三级缓存结构
所谓三级缓存,在Spring中表现为三个Map对象,这三个对象定义在DefaultSingletoBeanRegister类中,该类时DefaultListableBeanFactory的父类。以下源码为DefaultSingletonBeanRegister中的三级缓存Map定义代码。
//单例对象的缓存
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
//单例对象工厂的缓存
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
//提前暴露的单例对象的缓存
private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);
请注意,这里的singletonObjects变量就是第一级缓存,用来持有完成的Bean实例。而earlySingletonObjects中存在的那些提前暴露的对象,也就是已经创建但还没有完成属性注入的对象,属于第二级缓存。最后的singletonFactory存放用来创建earlySingletonObject的工厂对象,属于第三级缓存。
那么,三级缓存是如何发挥作用的呢?让我们来分析获取Bean的代码流程。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//首先从一级缓存singletonObjects中获取
Object singletonObject = this.singletonObjects.get(beanName);
//如果获取不到,就从二级缓存earlySingletonObjects中获取
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
synchronized (this.singletonObjects) {
// 如果还获取不到,就从三级缓存singletonFactory中获取
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
//一旦获取成功,就把对象从三级缓存移动到第二级缓存中
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
}
}
return singletonObject;
}
看了这段代码,不难理解对三级缓存的一次访问过程,但可能还是不理解Spring为什么要这样设计。事实上,解决循环依赖的关键还是要围绕Bean的生命周期。在前面介绍Bean的实例化时,我们知道它包含三个核心步骤,而在第一步和第二部之间,存在一个addSingletonFactory()方法,源码如下:AbstractAutowireCapableBeanFactory类的doCreateBean方法
//1 初始化Bean,通过构造器创建Bean
instanceWrapper = createBeanInstance(beanName, mbd, args);
//针对循环依赖问题暴露单例工厂类
addSingletonFactory(beanName, () -> getEarlyBeanReference(

最低0.47元/天 解锁文章
8万+

被折叠的 条评论
为什么被折叠?



