spring bean的循环依赖的解决

本文详细描述了Spring框架中DService和D1Service实例之间的依赖注入过程,涉及bean实例化、缓存机制及Autowired注解的作用。
摘要由CSDN通过智能技术生成

提前说明

DService实例属性注入D1Service实例,D1Service实例属性注入DService实例,通过调试看spring是如何解决DService实例与D1Service实例的相互依赖的情况的。

在bean实例化时,先实例化D1Service。

D1Service对象实例化

org.springframework.beans.BeanUtils

D1Service 对象被添加到三级缓存singletonFactories中

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

D1Service 在属性注入时,获取DService的属性值

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

获取DService的bean实例

org.springframework.beans.factory.support.AbstractBeanFactory

DService 对象的实例化

org.springframework.beans.BeanUtils

DService 对象被添加到一级缓存singletonFactories中

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

DService 在属性注入时,获取D1Service的属性值

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

org.springframework.beans.factory.support.DefaultListableBeanFactory

获取D1Service的bean实例

org.springframework.beans.factory.support.DefaultListableBeanFactory

org.springframework.beans.factory.support.AbstractBeanFactory

尝试从一、二、三级缓存中获取D1Service

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

执行getEarlyBeanReference方法获取D1Service对象

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter

 将D1Service的对象从三级缓存中取出,放到二级缓存中

返回d1Service未初始化的对象

Dservice完成d1Service属性的注入

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

DService进行实例化

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

DService放入一级缓存,并从一二级缓存删除

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值