spring循环依赖
如何底层解决循环依赖原理
Map<String, Object> singletonObjects 一级缓存完成对象(对象和属性都已经赋值)
Map<String, Object> earlySingletonObjects 二级缓存 不完整对象 对象已经创建但是属性缺没有赋值 早期对象
Map<String, ObjectFactory<?>> singletonFactories 三级缓存(只初始化,没有给属性赋值对象) 存放提前曝光对象
对象循环引用 (java的引用传递)
HashMap<Object, Object> objectHashMap = new HashMap<Object, Object>();
// 1.添加A对象
objectHashMap.put("aService", new AService());
// 2.B对象从三级缓存中查找到B
objectHashMap.put("bService", new BService());
AService aService = (AService) objectHashMap.get("aService");
BService bService = (BService) objectHashMap.get("bService");
bService.aService = aService;
//3.A对象需要设置
aService.bService = bService;
spring 对象初始化过程
- doGetBean 创建对象
- getSingleton
String beanName 是否有缓存对象,如果有直接返回。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 1.从一级缓存中查询是否存在该对象 查询完整的对象
Object singletonObject = this.singletonObjects.get(beanName);
//2.没有创建对象,并且该对象正在创建(标记为循环依赖的查询)
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
// 3.查询二级缓存中是否存在对象
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
// 4.查询三级缓存是否存在对象,如果存在放入到二级缓存中。
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
- getSingleton --> beforeSingletonCreation(beanName);
// 标记对象是否开始创建
private final Set<String> singletonsCurrentlyInCreation =
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
- createBean→doCreateBean创建对象
- createBeanInstance 基于反射或者cglib创建对象(注意对象不完整,因为属性没有设置的)
- A将不完整对象(婴儿对象)设置到我们的三级缓存中。
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
- populateBean给对象属性赋值(检查到现在依赖B对象,这时候肯定需要创建B对象)
- 继续走duGetBean 1-6流程 发现B对象中依赖与A对象
- 继续调用getSingleton方法 A对象已经在三级缓存中存在数据,直接获取三级缓存中。
applyPropertyValues →valueResolver.resolveValueIfNecessary →
resolveReference(argName, ref) - 调用自定义init方法 (初始化)