spring三級緩存:
* singletonObjects 一級緩存,用于保存實例化、注入、初始化完成的bean實例
* earlySingletonObjects 二級緩存,用于保存**實例化(但未初始化)**完成的bean實例
* singletonFactories 三級緩存,用于保存bean創建工廠(ObjectFactory),以便于后面擴展有機會創建代理對象
```
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of singleton factories: bean name --> ObjectFactory */
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */
private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
```
獲取單例Bean的源碼如下:
1. 先從一級緩存singletonObjects中去獲取。(如果獲取到就直接return)
2. 如果獲取不到或者對象正在創建中(isSingletonCurrentlyInCreation()),那就再從二級緩存earlySingletonObjects中獲取。(如果獲取到就直接return)
3. 如果還是獲取不到,且允許singletonFactories(allowEarlyReference=true)通過getObject()獲取。就從三級緩存singletonFactory.getObject()獲取。(如果獲取到了就從singletonFactories中移除,并且放進earlySingletonObjects)
> 加入`singletonFactories`三級緩存的前提是執行了構造器,所以構造器的循環依賴沒法解決
```
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
...
@Override
@Nullable
public Object getSingleton(String beanName) {
return getSingleton(beanName, true);
}
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
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;
}
...
public boolean isSingletonCurrentlyInCreation(String beanName) {
return this.singletonsCurrentlyInCreation.contains(beanName);
}
protected boolean isActuallyInCreation(String beanName) {
return isSingletonCurrentlyInCreation(beanName);
}
...
}
```
```
// 它可以將創建對象的步驟封裝到ObjectFactory中 交給自定義的Scope來選擇是否需要創建對象來靈活的實現scope。 具體參見Scope接口
@FunctionalInterface
public interface ObjectFactory<T> {
T getObject() throws BeansException;
}
```
經過ObjectFactory.getObject()后,此時放進了二級緩存**`earlySingletonObjects`**內。這個時候對象已經實例化了,**`雖然還不完美`**,但是對象的引用已經可以被其它引用了
- 概述
- Spring的使命
- 環境變量
- spring架構
- Spring各版本特性
- Spring3.1新特性
- spring頂級項目
- spring基礎
- spring環境變量
- 依賴注入
- spring獲取bean方法
- BeanFactory vs FactoryBean
- JavaBean裝配
- XML顯式配置
- 基于JAVA配置
- 自動化裝配bean
- SpringBean的作用域
- Spring應用上下文實現
- springbean的生命周期
- 自定義Bean的創建與銷毀
- Spring容器啟動過程
- spring加載xsd文件的流程
- spring擴展接口
- Spring主要類功能說明
- spring事務管理
- 事務特性
- 數據庫事務隔離級別
- 事務隔離性問題
- spring事務隔離級別
- 事務傳播行為
- @Transactional
- 循環依賴
- 構造器注入
- 循環依賴原理
- spring循環依賴原理
- spring三級緩存
- Spring注解
- @Component
- @ComponentScan
- @Autowired
- @Import
- @ImportResource
- @Profile
- @Conditional
- @Qualifier
- @Scope
- @PropertySource
- @Value
- @EnableScheduling
- SpEL-Spring表達式
- Spring-AOP
- SpringAOP五種通知類型
- AOP術語
- SpringMVC
- MVC原理圖
- SpringMVC工作原理
- springboot
- @SpringCloudApplication
- springboot tomcat配置
- Spring Boot Starter POMs
- Spring Boot technical starters
- spring boot事件類型
- Springboot日志
- SpringCloud
- springcloud微服務解決方案
- 服務組件
- 注冊中心
- Eureka
- Spring Cloud Zookeeper
- nacos
- Hystrix熔斷原理
- Hystrix應用
- Spring Cloud Config
- 服務網關
- Zuul
- Spring Cloud Gateway
- 服務調用及負載
- Ribbon
- Feign&OpenFeign
- Turbine
- actuator
- springboot & springcloud
- springcloud vs dubbo
- 常見面試題
- BeanFactory和FactoryBean
- @Autowired/@Resource和@Inject的區別
- Singleton bean注入prototype bean
- 附錄