/**一級緩存,用於存放徹底初始化好的 bean,從該緩存中取出的 bean 能夠直接使用*/
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
/**三級緩存 存放 bean 工廠對象,用於解決循環依賴*/
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
/**二級緩存 存放原始的 bean 對象(還沒有填充屬性),用於解決循環依賴*/
private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
複製代碼
【1】 getSingleton(beanName):源碼以下:java
//查詢緩存
Object sharedInstance = getSingleton(beanName);
//緩存中存在而且args是null
if (sharedInstance != null && args == null) {
//.......省略部分代碼
//直接獲取Bean實例
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
//getSingleton源碼,DefaultSingletonBeanRegistry#getSingleton
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//先從一級緩存中獲取已經實例化屬性賦值完成的Bean
Object singletonObject = this.singletonObjects.get(beanName);
//一級緩存不存在,而且Bean正處於建立的過程當中
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
//從二級緩存中查詢,獲取Bean的早期引用,實例化完成可是未賦值完成的Bean
singletonObject = this.earlySingletonObjects.get(beanName);
//二級緩存中不存在,而且容許建立早期引用(二級緩存中添加)
if (singletonObject == null && allowEarlyReference) {
//從三級緩存中查詢,實例化完成,屬性未裝配完成
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//二級緩存中添加
this.earlySingletonObjects.put(beanName, singletonObject);
//從三級緩存中移除
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
複製代碼
【2】addSingletonFactory:源碼以下spring
//中間省略部分代碼。。。。。
//建立Bean的源碼,在AbstractAutowireCapableBeanFactory#doCreateBean方法中
if (instanceWrapper == null) {
//實例化Bean
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//容許提早暴露
if (earlySingletonExposure) {
//添加到三級緩存中
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
try {
//屬性裝配,屬性賦值的時候,若是有發現屬性引用了另一個Bean,則調用getBean方法
populateBean(beanName, mbd, instanceWrapper);
//初始化Bean,調用init-method,afterproperties方法等操做
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
//添加到三級緩存的源碼,在DefaultSingletonBeanRegistry#addSingletonFactory
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
synchronized (this.singletonObjects) {
//一級緩存中不存在
if (!this.singletonObjects.containsKey(beanName)) {
//放入三級緩存
this.singletonFactories.put(beanName, singletonFactory);
//從二級緩存中移除,
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
複製代碼
【3】addSingleton:源碼以下:緩存
//獲取單例對象的方法,DefaultSingletonBeanRegistry#getSingleton
//調用createBean實例化Bean
singletonObject = singletonFactory.getObject();
//。。。。中間省略部分代碼
//doCreateBean以後才調用,實例化,屬性賦值完成的Bean裝入一級緩存,能夠直接使用的Bean
addSingleton(beanName, singletonObject);
//addSingleton源碼,在DefaultSingletonBeanRegistry#addSingleton方法中
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
//一級緩存中添加
this.singletonObjects.put(beanName, singletonObject);
//移除三級緩存
this.singletonFactories.remove(beanName);
//移除二級緩存
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
複製代碼
【1】爲何 Spring 不能解決構造器的循環依賴?bash
【2】爲何多實例 Bean 不能解決循環依賴?微信