Spring源码解析-三级缓存与循环依赖

// 如果两个条件满足,说明出现了循环依赖

if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {

synchronized (this.singletonObjects) {

// 再从二级缓存中获取

singletonObject = this.earlySingletonObjects.get(beanName);

// 这里又有两个判断

// singletonObject == null 表示在二级缓存中没有获取到

// allowEarlyReference 这个值传进来就为true,表示是否允许获取早期引用

// 如果两个条件都满足,就到三级缓存中获取

if (singletonObject == null && allowEarlyReference) {

// 从三级缓存获取,注意,这里获取到是一个ObjectFactory

ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);

if (singletonFactory != null) {

// 通过ObjectFactory获取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 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 be null”);

synchronized (this.singletonObjects) {

// 从一级缓存中获取bean

Object singletonObject = this.singletonObjects.get(beanName);

// 如果bean为空

if (singletonObject == null) {

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 '” + be

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值