spring如何解决循环依赖问题详解
循环依赖其实就是循环引用,很多地方都说需要两个或则两个以上的bean互相持有对方最终形成闭环才是循环依赖,比如A依赖于B,B依赖于C,C又依赖于A。其实一个bean持有自己类型的属性也会产生循环依赖。
settersingleton循环依赖
使用
SingleSetterBeanA依赖SingleSetterBeanB,SingleSetterBeanB依赖SingleSetterBeanA。
@Data publicclassSingleSetterBeanA{ @Autowired privateSingleSetterBeanBsingleSetterBeanB; }
@Data publicclassSingleSetterBeanB{ @Autowired privateSingleSetterBeanAsingleSetterBeanA; }
源码分析
spring是通过三级缓存来解决循环依赖的,那么三级缓存是怎么工作的呢?
三级缓存对应org.springframework.beans.factory.support.DefaultSingletonBeanRegistry类的三个属性:
/**Cacheofsingletonobjects:beannametobeaninstance.*/ privatefinalMapsingletonObjects=newConcurrentHashMap<>(256);//一级缓存 /**Cacheofsingletonfactories:beannametoObjectFactory.*/ privatefinalMap >singletonFactories=newHashMap<>(16);//二级缓存 /**Cacheofearlysingletonobjects:beannametobeaninstance.*/ privatefinalMap earlySingletonObjects=newConcurrentHashMap<>(16);//三级缓存
对于setter注入造成的依赖是通过Spring容器提前暴露刚完成实例化但未完成初始化的bean来完成的,而且只能解决单例作用域的bean循环依赖。通过提前暴露一个单例工厂方法,从而使其他bean能引用到该bean,关键源码如下所示:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
//处理循环依赖,实例化后放入三级缓存 booleanearlySingletonExposure=(mbd.isSingleton()&&this.allowCircularReferences&& isSingletonCurrentlyInCreation(beanName)); if(earlySingletonExposure){ if(logger.isTraceEnabled()){ logger.trace("Eagerlycachingbean'"+beanName+ "'toallowforresolvingpotentialcircularreferences"); } addSingletonFactory(beanName,()->getEarlyBeanReference(beanName,mbd,bean)); }
bean实例化后放入三级缓存中:
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingletonFactory
protectedvoidaddSingletonFactory(StringbeanName,ObjectFactory>singletonFactory){ Assert.notNull(singletonFactory,"Singletonfactorymustnotbenull"); synchronized(this.singletonObjects){ if(!this.singletonObjects.containsKey(beanName)){ this.singletonFactories.put(beanName,singletonFactory);//三级缓存 this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }
放入三级缓存中的是ObjectFactory类型的lambda表达式:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#getEarlyBeanReference
protectedObjectgetEarlyBeanReference(StringbeanName,RootBeanDefinitionmbd,Objectbean){ ObjectexposedObject=bean; if(!mbd.isSynthetic()&&hasInstantiationAwareBeanPostProcessors()){ for(BeanPostProcessorbp:getBeanPostProcessors()){ if(bpinstanceofSmartInstantiationAwareBeanPostProcessor){ SmartInstantiationAwareBeanPostProcessoribp=(SmartInstantiationAwareBeanPostProcessor)bp; /** *@seeorg.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getEarlyBeanReference(java.lang.Object,java.lang.String) */ //使用AbstractAutoProxyCreator#getEarlyBeanReference创建代理对象 exposedObject=ibp.getEarlyBeanReference(exposedObject,beanName); } } } returnexposedObject; }
构造器参数循环依赖
通过构造器注入构成的循环依赖,此依赖是无法解决的,只能抛出BeanCurrentlyInCreationException异常表示循环依赖。
使用
@Data publicclassSingleConstrutorBeanA{ publicSingleConstrutorBeanA(SingleConstrutorBeanBsingleConstrutorBeanB){ } }
@Data publicclassSingleConstrutorBeanB{ publicSingleConstrutorBeanB(SingleConstrutorBeanAsingleConstrutorBeanA){ } }
上面的代码运行时会抛出如下异常:
...... Causedby:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'singleConstrutorBeanB':Unsatisfieddependencyexpressedthroughconstructorparameter0;nestedexceptionisorg.springframework.beans.factory.BeanCurrentlyInCreationException:Errorcreatingbeanwithname'singleConstrutorBeanA':Requestedbeaniscurrentlyincreation:Isthereanunresolvablecircularreference? atorg.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:805) atorg.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1403) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1245) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:538) atorg.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:329) atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) atorg.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240) atorg.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:892) atorg.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:796) ...76more Causedby:org.springframework.beans.factory.BeanCurrentlyInCreationException:Errorcreatingbeanwithname'singleConstrutorBeanA':Requestedbeaniscurrentlyincreation:Isthereanunresolvablecircularreference? atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355) atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227) atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) atorg.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240) atorg.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:892) atorg.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:796) ...90more
源码分析
Spring容器会将每一个正在创建的Bean标识符放在一个“当前创建Bean池”中,Bean标识符在创建过程中将一直保持在这个池中,因此如果在创建Bean过程中发现自己已经在“当前创建Bean池”里时将抛出BeanCurrentlyInCreationException异常表示循环依赖;而对于创建完毕的Bean将从“当前创建Bean池”中清除掉。
protectedvoidbeforeSingletonCreation(StringbeanName){ if(!this.inCreationCheckExclusions.contains(beanName)&&!this.singletonsCurrentlyInCreation.add(beanName)){ thrownewBeanCurrentlyInCreationException(beanName); } }
@Lazy打破循环依赖
在上面的例子中只需要在SingleConstrutorBeanA或者SingleConstrutorBeanB的构造方法上面加上@Lazy注解,就会发现不会抛出异常了,这又是为什么呢?
下面假设在SingleConstrutorBeanA的构造方法上面加了@Lazy注解,在构造B时,发现参数A时被@Lazy注解修饰时,那么就不会调用getBean来获取对象,而是创建了一个代理对象,所以不会构成真正的循环依赖,不会抛出BeanCurrentlyInCreationException异常。
/** *处理懒加载对象 *懒加载返回的又是一个代理对象,不会真正的调用getBean,所以如果构造方法依赖中有循环依赖,那么不会报错 *@seeorg.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#getLazyResolutionProxyIfNecessary(org.springframework.beans.factory.config.DependencyDescriptor,java.lang.String) */ Objectresult=getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary( descriptor,requestingBeanName); if(result==null){ //调用beanFactory.getBean(beanName)从容器中获取依赖对象 result=doResolveDependency(descriptor,requestingBeanName,autowiredBeanNames,typeConverter); } returnresult;
setterprototype循环依赖
对于prototype作用域bean,Spring容器无法完成依赖注入,因为Spring容器不进行缓存"prototype"作用域的bean,因此无法提前暴露一个创建中的bean。
使用
@Data @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) publicclassPrototypeBeanA{ @Autowired privatePrototypeBeanBprototypeBeanB; }
@Data @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) publicclassPrototypeBeanB{ @Autowired privatePrototypeBeanAprototypeBeanA; }
@Test publicvoidtest3(){ AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext(); applicationContext.register(PrototypeBeanA.class); applicationContext.register(PrototypeBeanB.class); applicationContext.refresh(); applicationContext.getBean(PrototypeBeanA.class);//此时必须要获取Spring管理的实例,因为现在scope="prototype"只有请求获取的时候才会实例化对象 }
运行结果如下:
Causedby:org.springframework.beans.factory.BeanCurrentlyInCreationException:Errorcreatingbeanwithname'prototypeBeanA':Requestedbeaniscurrentlyincreation:Isthereanunresolvablecircularreference? atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:269) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) atorg.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1322) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240) atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:668) ...89more
源码分析
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
...... //判断是否在当前创建Bean池中 if(isPrototypeCurrentlyInCreation(beanName)){ thrownewBeanCurrentlyInCreationException(beanName); } ......
异常就是在上面的代码中抛出来的,那么beanName是什么时候添加至当前创建Bean池中的呢?
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
elseif(mbd.isPrototype()){ //It'saprototype->createanewinstance. //prototype类型的bean的实例化 ObjectprototypeInstance=null; try{ beforePrototypeCreation(beanName); prototypeInstance=createBean(beanName,mbd,args); } finally{ afterPrototypeCreation(beanName); } bean=getObjectForBeanInstance(prototypeInstance,name,beanName,mbd); }
org.springframework.beans.factory.support.AbstractBeanFactory#beforePrototypeCreation
protectedvoidbeforePrototypeCreation(StringbeanName){ //ThreadLocal ObjectcurVal=this.prototypesCurrentlyInCreation.get(); if(curVal==null){ this.prototypesCurrentlyInCreation.set(beanName); } elseif(curValinstanceofString){ SetbeanNameSet=newHashSet<>(2); beanNameSet.add((String)curVal); beanNameSet.add(beanName); this.prototypesCurrentlyInCreation.set(beanNameSet); } else{ Set beanNameSet=(Set )curVal; beanNameSet.add(beanName); } }
其根本原因就是Spring容器不会对prototype类型的bean进行缓存,因此无法提前利用三级缓存暴露一个代理对象。
循环依赖开关
可以通过allowCircularReferences来禁止循环依赖,这样的话,singletonbean的setter循环依赖也会报错。
二级缓存可行?
缓存 | 说明 |
---|---|
singletonObjects | 第一级缓存,存放可用的成品Bean。 |
earlySingletonObjects | 第二级缓存,存放半成品的Bean,半成品的Bean是已创建对象,但是未注入属性和初始化,用以解决循环依赖。 |
singletonFactories | 第三级缓存,存的是Bean工厂对象,用来生成半成品的Bean并放入到二级缓存中,用以解决循环依赖。 |
理论上二级缓存时可行的,只需要将三级缓存中BeanFactory创建的对象提前放入二级缓存中,这样三级缓存就可以移除了。
那么spring中为什么还要使用三级缓存呢?如果要使用二级缓存解决循环依赖,意味着所有Bean在实例化后就要完成AOP代理,这样违背了Spring设计的原则,Spring在设计之初就是通过AnnotationAwareAspectJAutoProxyCreator这个后置处理器来在Bean生命周期的最后一步来完成AOP代理,而不是在实例化后就立马进行AOP代理。
前言
到此这篇关于spring如何解决循环依赖问题的文章就介绍到这了,更多相关spring循环依赖内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。