Spring 三级缓存和循环依赖 思考和总结
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 的活动过程。
============================================================================
我们在之前介绍 Bean 的生命周期时说过,spring 中 bean 的实例化过程,并非只是调用构造方法。除去 spring 框架本身提供的一些钩子或扩展方法,简单分成下面三个核心方法:
Spring 在创建 Bean 的过程中分为三步
实例化,对应方法:AbstractAutowireCapableBeanFactory 中的 createBeanInstance 方法,简单理解就是 new 了一个对象。
属性注入,对应方法:AbstractAutowireCapableBeanFactory 的 populateBean 方法,为实例化中 new 出来的对象填充属性和注入依赖。
初始化,对应方法:AbstractAutowireCapableBeanFactory 的 initializeBean,执行 aware 接口中的方法,初始化方法,完成 AOP 代理。
从单例 Bean 的初始化来看,主要可能发生循环依赖的环节就在第二步 populate 。值得注意的是, 基于构造方法注入 的方式,其实是将第一步和第二步同时进行,因此马上就抛出错误。而 spring 通过 基于属性注入 的方式,是否有其他特殊的处理呢,我们这时候就要提到 spring 的三级缓存:
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
=========================================================================================
我们在获取 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 呢?
==============================================================
===========================
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 的实例化过程:
评论