闽公网安备 35020302035485号

public class PersonA {
@Autowired
private PersonB personB;
}
public class PersonB {
@Autowired
private PersonA personA;
}
2.2 Spring 处理循环依赖的前提条件/** 一级缓存 */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256); /** 二级缓存 */ private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16); /** 三级缓存 */ private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);3.2 三级缓存流程

protected <T> T doGetBean( String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException {
// 获取bean的规范名称
String beanName = transformedBeanName(name);
Object bean;
// 从各级缓存中获取bean对象
Object sharedInstance = getSingleton(beanName);
// 跟factoryBean相关判断
if (sharedInstance != null && args == null) {
...
}
// 获取factorybean的真是bean
//若为普通bean则直接返回对象
// 堆代码 duidaima.com
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
......
// 创建单利bean对象
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
// 创建bean
return createBean(beanName, mbd, args);
} catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean. destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
......
// 返回bean对象 return (T) bean; }
我们看两个比较重要获取 Bean 的方法 GetSingleton()protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 从一级缓存中查找bean
Object singletonObject = this.singletonObjects.get(beanName);
// 判断一级缓存查找不到bean && bean是否处于创建中,成立,则进入循环依赖
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
// 从二级缓存中查找
singletonObject = this.earlySingletonObjects.get(beanName);
// 二级缓存未查询到 && 是否允许获取早期引用
if (singletonObject == null && allowEarlyReference) {
// 从三级缓存查询
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
// 三级缓存存在bean
if (singletonFactory != null) {
// 获取bean实例
singletonObject = singletonFactory.getObject();
// 从三级缓存升级到二级缓存,
this.earlySingletonObjects.put(beanName, singletonObject);
// 三级缓存中移除
this.singletonFactories.remove(beanName);
}
}
}
}
// 返回bean
return singletonObject;
}
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
// 从一级缓存中获取对应bean
Object singletonObject = this.singletonObjects.get(beanName);
// 若bean不存在
if (singletonObject == null) {
// 当前正在销毁bean,不能创建
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
// 创建前检查,记录正在加载状态
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
// 如果当前没有异常,初始化异常集合
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
// 执行匿名内部类方法
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
catch(IllegalStateException ex){
// 执行getObject方法创建bean
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
throw ex;
}
} catch(BeanCreationException ex){
if (recordSuppressedExceptions) {
or (Exception suppressedException : this.suppressedExceptions) {
ex.addRelatedCause(suppressedException);
}
}
throw ex;
} finally{
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
// 单例bean创建完成后,容器移除bean
afterSingletonCreation(beanName);
}
// newSingleton为true时,表示bean创建成功
// 判断是否为新的完成整bean
if (newSingleton) {
// 将bean存入一级缓存中
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}
添加与清理缓存// 将bean放入一级缓存切清楚二级、三级缓存
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {xw
// 添加到一级缓存中
this.singletonObjects.put(beanName, singletonObject);
// 从三级缓存中移除
this.singletonFactories.remove(beanName);
// 从二级缓存中移除
this.earlySingletonObjects.remove(beanName);
// 放入已注册的单利池中
this.registeredSingletons.add(beanName);
}
}
// 添加到三级缓存
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
synchronized (this.singletonObjects) {
// 若一级缓存不存在bean实例
if (!this.singletonObjects.containsKey(beanName)) {
// 添加到三级缓存
this.singletonFactories.put(beanName, singletonFactory);
// 从第二级缓存删除
this.earlySingletonObjects.remove(beanName);
// 放入已注册的单例池里
this.registeredSingletons.add(beanName);
}
}
四.总结1、一级缓存SingletonObjects实例化、初始化实例。
2、二级缓存EarlySingletonObjects存放的是早期的 Bean ,半成品还未初始化的 bean。
3、三级缓存SingletonFactories是一个对象工厂,用于创建对象,然后放入到二级缓存中。同时对象如果存在 Aop 代理,那么返回的对象就是代理对象。