SpringIOC DI循环依赖实例详解

时间:2021-05-19

要弄清楚循环依赖

1、需要知道Bean初始化的两个阶段

① Bean实例化创建实例对象(new Bean())

② Bean实例对象初始化(DI:注解自动注入)

2、DefaultSingletonBeanRegistry类中的5个容器

/** 记录已将创建的单例<beanName,singletonBean> */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); /** 记录singletonFactory<beanName,singletonFactory> singeletonFactory中存放beanName和上面的①阶段的bean:Bean实例化实例对象(还未初始化DI)*/ private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16); /** 记录早期的singletonBean 存放的也是① */ private final Map<String, Object> earlySingletonObjects = new HashMap<>(16); /** 存放已经初始化后的beanName,有序 */ private final Set<String> registeredSingletons = new LinkedHashSet<>(256); /** 记录正在初始化的bean的beanName */ private final Set<String> singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap<>(16));

其中跟循环依赖相关的是singletonFactories、singeletonsCurrentlyInCreation、earlysingletonObjects.

3、循环依赖实现

①bean初始化前后会打标,加入到singletonsCurrentlyInCreation容器中,这个打标会在核心方法getSingleton()中起作用

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { ... //循环依赖相关:初始化前先singletonsCurrentlyInCreation.add(beanName) beforeSingletonCreation(beanName); ... //lamda表达式:其实是调用createBean(beanName, mbd, args):Bean初始化 singletonObject = singletonFactory.getObject(); ... //循环依赖相关:初始化后singletonsCurrentlyInCreation.remove(beanName) afterSingletonCreation(beanName); ...//初始化完后 //this.singletonObjects.put(beanName, singletonObject);放入到单例容器中 //this.singletonFactories.remove(beanName);清空循环依赖的两个打标 //this.earlySingletonObjects.remove(beanName); //this.registeredSingletons.add(beanName);放入单例beanName容器中 addSingleton(beanName, singletonObject); ... } }

② 上面singletonObject = singletonFactory.getObject()时向singletonFactories中记录了(new Bean()),singletonFactories也会在核心方法getSingleton()中起作用

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { ... //循环依赖相关逻辑: //this.singletonFactories.put(beanName, singletonFactory); //将实例化bean(①阶段)、beanName组装成singletonFactory装入singletonFactories容器 //this.earlySingletonObjects.remove(beanName); //删除earlySingletonObjects中beanName addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); ... //实例初始化 就是在这里面实现依赖注入DI的:反射实现 //调用AutowiredAnnotationBeanPostProcessor.postProcessProperties populateBean(beanName, mbd, instanceWrapper); ... }

③ 核心方法getSingleton

protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName);     //循环依赖核心就在于这个判断,由于打标+记录了①阶段的bean, //循环依赖第二次调用getBean("a")时,这里会直接返回第一次调用getBean("a")创建的①阶段的bean //而不会调用createBean("a")再次bean初始化(造成两个bean的循环创建) 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; }

④ 循环依赖流程

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { ... //假设A、B互相依赖 //第一次getBean(A),sharedInstance == null,走else,createBean //A正在创建打标,①中beforeSingletonCreation() //A实例化后保存到singletonFactories中②中addSingletonFactory(beanName,singletonFactory) //DI依赖注入:②中populateBean(beanName,mbd,instanceWrapper),发现依赖B,调用getBean(B)初始化B的单例 //调用getBean(B)重复上面步骤,DI依赖注入发现依赖A,调用getBean(A) //第二次getBean(A),③中if(singletonObject == null && isSingletonCurrentlyInCreation(A))由于打标了所以返回singleFactory.getObject() //下面if条件直接返回bean,没有走else破坏了循环 Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { // bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { ... // createBean(beanName, mbd, args); bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } return bean; }

四、总结

未看源码之前,其实对循环依赖有一个想法:循环依赖可以看做是一个死锁。

预防死锁的方法:打破死锁的四个必要条件(互斥、请求并等待、不可剥夺、循环等待),由于循环依赖的资源是对象自身,所以常用破坏循环等待条件方法:编号顺序执行,不适用

选择破坏请求并等待条件:先创建对象,再赋值,模型

A a = new A();
B b = new B();
A.b = b;
B.a = a;

研究源码之后发现:想法差不多,但是代码实现非常精彩。模型(打标没想到过)

A a = new A();
B b = new B();
b.a = a;
a.b = b;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章