Spring怎么解决循环依赖的问题?

编程入门 行业动态 更新时间:2024-10-18 16:42:35

<a href=https://www.elefans.com/category/jswz/34/1769862.html style=Spring怎么解决循环依赖的问题?"/>

Spring怎么解决循环依赖的问题?

spring bean有两种 注入方式

对于构造器注入的循环依赖,Spring处理不了,会直接抛出BeanCurrentlylnCreationException异常。

对于属性注入的循环依赖(单例模式下),是通过三级缓存处理来循环依赖的。

singletonObjects 一级缓存 主要存放Spring完整生命周期的单例bean

earlySingletonObjects二级缓存 主要存放完成实例化未初始化的单例对象map,bean name -->

singletonFactories : 单例对象工厂map,bean name --> ObjectFactory,单例对象实例化完成之后会加入singletonFactories

spring 容器在获取bean的时候首先从一级缓存总获取,获取不到,到二级缓存获取,如果获取不到到三级缓存获取,如果从三级缓存获取到了就会从三级缓存中删除缓存,然后将从三级缓存获取的半成品bean放到二级缓存

protected Object getSingleton(String beanName, boolean allowEarlyReference) {//从一级缓存中获取Object singletonObject = this.singletonObjects.get(beanName);//若一级缓存中没有,且当前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) {/*** 从对象工厂中获取到一个半成品bean*/singletonObject = singletonFactory.getObject();//放入到二级缓存this.earlySingletonObjects.put(beanName, singletonObject);//从三级缓存中移除this.singletonFactories.remove(beanName);}}}}return singletonObject;
}

在获取bean的时候,添加三级缓存

	boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isDebugEnabled()) {logger.debug("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references");}addSingletonFactory(beanName, new ObjectFactory<Object>() {@Overridepublic Object getObject() throws BeansException {return getEarlyBeanReference(beanName, mbd, bean);}});}

更多推荐

Spring怎么解决循环依赖的问题?

本文发布于:2023-12-07 10:42:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1671027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!