Spring三級緩存解決循環(huán)依賴的解析過程
一、循環(huán)依賴場景
假設存在兩個Bean的相互依賴:
@Component public class ServiceA { @Autowired private ServiceB serviceB; } @Component public class ServiceB { @Autowired private ServiceA serviceA; }
二、三級緩存定義
在 DefaultSingletonBeanRegistry
中定義:
// 一級緩存:完整Bean(K:Bean名稱 V:實例化+初始化完成的Bean) private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); // 二級緩存:早期暴露對象(K:Bean名稱 V:未完成屬性注入的原始Bean) private final Map<String, Object> earlySingletonObjects = new HashMap<>(16); // 三級緩存:對象工廠(K:Bean名稱 V:ObjectFactory) private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
三、解決流程(以ServiceA和ServiceB為例)
1. 創(chuàng)建ServiceA
sequenceDiagram participant Container participant Cache1 as singletonObjects participant Cache2 as earlySingletonObjects participant Cache3 as singletonFactories Container->>Cache1: 檢查ServiceA是否存在 Cache1-->>Container: 不存在 Container->>Container: 實例化ServiceA(構造器調用) Container->>Cache3: 添加ServiceA的ObjectFactory Container->>Container: 開始屬性注入(需要ServiceB)
關鍵代碼段:
// AbstractAutowireCapableBeanFactory#doCreateBean addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
2. 發(fā)現需要ServiceB
sequenceDiagram participant Container participant Cache1 participant Cache2 participant Cache3 Container->>Cache1: 檢查ServiceB是否存在 Cache1-->>Container: 不存在 Container->>Container: 實例化ServiceB(構造器調用) Container->>Cache3: 添加ServiceB的ObjectFactory Container->>Container: 開始屬性注入(需要ServiceA)
3. 解決ServiceB對ServiceA的依賴
sequenceDiagram participant Container participant Cache1 participant Cache2 participant Cache3 Container->>Cache1: 查找ServiceA Cache1-->>Container: 不存在 Container->>Cache2: 查找ServiceA Cache2-->>Container: 不存在 Container->>Cache3: 獲取ServiceA的ObjectFactory Container->>Container: 執(zhí)行getEarlyBeanReference() Container->>Cache2: 將生成的代理對象存入earlySingletonObjects Container->>ServiceB: 注入ServiceA的早期引用 Container->>Container: 完成ServiceB初始化 Container->>Cache1: 將ServiceB放入singletonObjects
4. 回溯完成ServiceA初始化
sequenceDiagram participant Container participant Cache1 participant Cache2 participant Cache3 Container->>ServiceA: 注入已初始化的ServiceB Container->>Container: 執(zhí)行ServiceA的初始化后方法 Container->>Cache1: 將ServiceA放入singletonObjects Container->>Cache2: 移除ServiceA的早期引用 Container->>Cache3: 移除ServiceA的ObjectFactory
四、關鍵機制詳解
1. getEarlyBeanReference() 的核心作用
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { // 如果有必要,在此處生成代理對象 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; bean = ibp.getEarlyBeanReference(exposedObject, beanName); } } } return bean; }
2. 三級緩存必要性分析
緩存級別 | 解決的問題 | 典型場景 |
---|---|---|
singletonFactories | 處理AOP代理的延遲生成 | 需要保證代理對象的單例性 |
earlySingletonObjects | 避免重復創(chuàng)建早期引用 | 多個Bean依賴同一個未完成初始化的Bean |
singletonObjects | 存儲最終可用Bean | 正常Bean獲取 |
五、設計約束與限制
1.僅支持單例作用域
原型(prototype)作用域的Bean無法解決循環(huán)依賴,因為Spring不緩存原型Bean
2.構造器注入限制
如果循環(huán)依賴通過構造器注入發(fā)生,無法解決(實例化前就需要完成依賴注入)
3.異步初始化風險
使用@Async
等方法增強的Bean可能破壞初始化順序
六、調試技巧
查看緩存狀態(tài)
在 DefaultSingletonBeanRegistry
類中設置斷點:
// 查看三級緩存內容 System.out.println("singletonFactories: " + singletonFactories.keySet()); System.out.println("earlySingletonObjects: " + earlySingletonObjects.keySet()); System.out.println("singletonObjects: " + singletonObjects.keySet());
強制拋出循環(huán)依賴異常
在配置類添加:
@Bean public CircularReferencesBean circularReferencesBean() { return new CircularReferencesBean(circularReferencesBean()); }
七、性能優(yōu)化建議
避免過度使用循環(huán)依賴
即使技術可行,也應通過設計模式(如事件驅動)解耦
合理使用@Lazy注解
延遲加載非必要依賴:
@Autowired @Lazy private ServiceB serviceB;
監(jiān)控緩存命中率
通過JMX監(jiān)控 singletonObjects
與 earlySingletonObjects
的比例
總結
Spring的三級緩存機制通過 提前暴露對象引用 + 動態(tài)代理生成 的協(xié)同設計,在保證單例性的前提下,優(yōu)雅地解決了循環(huán)依賴問題。理解該機制需要重點把握Bean生命周期的階段劃分和緩存狀態(tài)的轉換邏輯。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring4.0 MVC請求json數據報406錯誤的解決方法
這篇文章主要為大家詳細介紹了Spring4.0 MVC請求json數據報406錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Windows下apache ant安裝、環(huán)境變量配置教程
這篇文章主要介紹了Windows下apache ant安裝、環(huán)境變量配置教程,ANT的安裝很簡單,本文同時講解了驗證安裝是否成功的方法和使用方法實例,需要的朋友可以參考下2015-06-06SpringBoot的ResponseEntity類返回給前端具體講解
這篇文章主要給大家介紹了關于SpringBoot的ResponseEntity類返回給前端的相關資料,ResponseEntity是Spring框架中用于封裝HTTP響應的類,可以自定義狀態(tài)碼、響應頭和響應體,常用于控制器方法中返回特定數據的HTTP響應,需要的朋友可以參考下2024-11-11Spring?Boot?+?EasyExcel?+?SqlServer?進行批量處理數據的高效方法
在日常開發(fā)和工作中,我們可能要根據用戶上傳的文件做一系列的處理,本篇文章就以Excel表格文件為例,主要介紹了Spring?Boot?+?EasyExcel?+?SqlServer?進行批量處理數據的高效方法,需要的朋友可以參考下2024-06-06Spring Security OAuth2實現使用JWT的示例代碼
這篇文章主要介紹了Spring Security OAuth2實現使用JWT的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09