终于把Spring三级缓存彻底看明白了
一. 前言
从简单到复杂应用开发中,难免会遇到spring bean循环依赖问题,即Bean A依赖Bean B, 同时Bean B依赖Bean A。Spring大神提出了三级缓存帮助我们解决循环依赖的问题。
二. 概念说明
singletonFactories :一级缓存,存放类工厂(ObjectFactory),实例Bean通过ObjectFactory创建。
earlySingletonObjects :二级缓存,存放创建不完全的Bean实例。
singletonObjects :三级缓存,存放实例化完成的可用Bean。
三. 三级缓存
AbstractBeanFactory的doGetBean方法创建Bean的入口
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
// 检查Bean是否已经存在
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
// 省略........
} else {
// 省略........
if (!typeCheckOnly) {
//标记Bean已经被创建
markBeanAsCreated(beanName);
}
try {
// 省略........
// 创建Bean
if (mbd.isSingleton()) {
// 调用父类DefaultSingletonBeanRegistry方法
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// 省略........
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
// 省略........
}
catch (BeansException ex) {
// 省略........
}
}
// 省略........
return (T) bean;
}
DefaultSingletonBeanRegistry类Bean的注册类,三级缓存所在类
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
// 标记Bean正在被创建
beforeSingletonCreation(beanName);
// 省略.....
try {
// 创建实例
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
catch (IllegalStateException ex) {
// 省略.....
}
catch (BeanCreationException ex) {
// 省略.....
}
finally {
// 省略.....
}
if (newSingleton) {
// Bean放入singletonObjects,并从singletonFactories, earlySingletonObjects中移除
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}
AbstractAutowireCapableBeanFactory
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// 省略.......
try {
// 创建Bean
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// 省略.......
}
catch (Throwable ex) {
// 省略.......
}
}
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// 创建实例
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 初始化Bean
Object exposedObject = bean;
try {
// 属性注入
populateBean(beanName, mbd, instanceWrapper);
// 调用实现InitializeBean接口的初始化方法
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
//省略.......
}
//省略.......
return exposedObject;
}
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
// 省略.......
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// bean名称注入
if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
// bean类型注入
if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
// 省略.....
}
protected void autowireByName(
String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
// 省略......
for (String propertyName : propertyNames) {
if (containsBean(propertyName)) {
// 获取依赖Bean实例
Object bean = getBean(propertyName);
pvs.add(propertyName, bean);
// 注入依赖
registerDependentBean(propertyName, beanName);
// 省略.....
}
else {
// 省略.....
}
}
}
DefaultSingletonBeanRegistry
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
// 主动注入Bean这里直接返回,依赖注入Bean才会执行里面逻辑
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
// singletonObjects没有找到,从earlySingletonObjects查找
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
// earlySingletonObjects没有找到则创建实例
singletonObject = singletonFactory.getObject();
// 创建的实例放入earlySingletonObjects,此时实例还没有进行属性注入
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
原文作者:#朱守成#
原文地址: https://blog.csdn.net/z562743237/article/details/121038960
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/z562743237/article/details/121038960
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章