spring如何快速稳定解决循环依赖问题

时间:2021-08-09 00:08:16

循环依赖其实就是循环引用,很多地方都说需要两个或则两个以上的bean互相持有对方最终形成闭环才是循环依赖,比如A依赖于B,B依赖于C,C又依赖于A。其实一个bean持有自己类型的属性也会产生循环依赖。

setter singleton循环依赖

使用

SingleSetterBeanA依赖SingleSetterBeanB,SingleSetterBeanB依赖SingleSetterBeanA。

  1. @Data
  2. public class SingleSetterBeanA {
  3. @Autowired
  4. private SingleSetterBeanB singleSetterBeanB;
  5. }
  1. @Data
  2. public class SingleSetterBeanB {
  3. @Autowired
  4. private SingleSetterBeanA singleSetterBeanA;
  5. }

源码分析

spring是通过三级缓存来解决循环依赖的,那么三级缓存是怎么工作的呢?

三级缓存对应org.springframework.beans.factory.support.DefaultSingletonBeanRegistry类的三个属性:

  1. /** Cache of singleton objects: bean name to bean instance. */
  2. private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); // 一级缓存
  3.  
  4. /** Cache of singleton factories: bean name to ObjectFactory. */
  5. private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16); // 二级缓存
  6.  
  7. /** Cache of early singleton objects: bean name to bean instance. */
  8. private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16); // 三级缓存

spring如何快速稳定解决循环依赖问题

对于setter注入造成的依赖是通过Spring容器提前暴露刚完成实例化但未完成初始化的bean来完成的,而且只能解决单例作用域的bean循环依赖。通过提前暴露一个单例工厂方法,从而使其他bean能引用到该bean,关键源码如下所示:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

  1. // 处理循环依赖,实例化后放入三级缓存
  2. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
  3. isSingletonCurrentlyInCreation(beanName));
  4. if (earlySingletonExposure) {
  5. if (logger.isTraceEnabled()) {
  6. logger.trace("Eagerly caching bean '" + beanName +
  7. "' to allow for resolving potential circular references");
  8. }
  9. addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
  10. }

bean实例化后放入三级缓存中:

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingletonFactory

  1. protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
  2. Assert.notNull(singletonFactory, "Singleton factory must not be null");
  3. synchronized (this.singletonObjects) {
  4. if (!this.singletonObjects.containsKey(beanName)) {
  5. this.singletonFactories.put(beanName, singletonFactory); // 三级缓存
  6. this.earlySingletonObjects.remove(beanName);
  7. this.registeredSingletons.add(beanName);
  8. }
  9. }
  10. }

放入三级缓存中的是ObjectFactory类型的lambda表达式:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#getEarlyBeanReference

  1. protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
  2. Object exposedObject = bean;
  3. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
  4. for (BeanPostProcessor bp : getBeanPostProcessors()) {
  5. if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
  6. SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
  7. /**
  8. * @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getEarlyBeanReference(java.lang.Object, java.lang.String)
  9. */
  10. // 使用AbstractAutoProxyCreator#getEarlyBeanReference创建代理对象
  11. exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
  12. }
  13. }
  14. }
  15. return exposedObject;
  16. }

构造器参数循环依赖

通过构造器注入构成的循环依赖,此依赖是无法解决的,只能抛出BeanCurrentlyInCreationException异常表示循环依赖。

使用

  1. @Data
  2. public class SingleConstrutorBeanA {
  3. public SingleConstrutorBeanA(SingleConstrutorBeanB singleConstrutorBeanB) {
  4. }
  5. }
  1. @Data
  2. public class SingleConstrutorBeanB {
  3. public SingleConstrutorBeanB(SingleConstrutorBeanA singleConstrutorBeanA) {
  4. }
  5. }

上面的代码运行时会抛出如下异常:

... ...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'singleConstrutorBeanB': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'singleConstrutorBeanA': Requested bean is currently in creation: Is there an unresolvable circular reference?
 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:805)
 at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1403)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1245)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:538)
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:329)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240)
 at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:892)
 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:796)
 ... 76 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'singleConstrutorBeanA': Requested bean is currently in creation: Is there an unresolvable circular reference?
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240)
 at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:892)
 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:796)
 ... 90 more

源码分析

Spring容器会将每一个正在创建的Bean标识符放在一个“当前创建Bean池”中,Bean标识符在创建过程中将一直保持在这个池中,因此如果在创建Bean过程中发现自己已经在“当前创建Bean池”里时将抛出BeanCurrentlyInCreationException异常表示循环依赖;而对于创建完毕的Bean将从“当前创建Bean池”中清除掉。

  1. protected void beforeSingletonCreation(String beanName) {
  2. if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
  3. throw new BeanCurrentlyInCreationException(beanName);
  4. }
  5. }

spring如何快速稳定解决循环依赖问题

@Lazy打破循环依赖

在上面的例子中只需要在SingleConstrutorBeanA或者SingleConstrutorBeanB的构造方法上面加上@Lazy注解,就会发现不会抛出异常了,这又是为什么呢?

下面假设在SingleConstrutorBeanA的构造方法上面加了@Lazy注解,在构造B时,发现参数A时被@Lazy注解修饰时,那么就不会调用getBean来获取对象,而是创建了一个代理对象,所以不会构成真正的循环依赖,不会抛出BeanCurrentlyInCreationException异常。

  1. /**
  2. * 处理懒加载对象
  3. * 懒加载返回的又是一个代理对象,不会真正的调用getBean,所以如果构造方法依赖中有循环依赖,那么不会报错
  4. * @see org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#getLazyResolutionProxyIfNecessary(org.springframework.beans.factory.config.DependencyDescriptor, java.lang.String)
  5. */
  6. Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
  7. descriptor, requestingBeanName);
  8. if (result == null) {
  9. // 调用beanFactory.getBean(beanName)从容器中获取依赖对象
  10. result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
  11. }
  12. return result;

spring如何快速稳定解决循环依赖问题

setter prototype循环依赖

对于prototype作用域bean,Spring容器无法完成依赖注入,因为Spring容器不进行缓存"prototype"作用域的bean,因此无法提前暴露一个创建中的bean。

使用

  1. @Data
  2. @Component
  3. @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  4. public class PrototypeBeanA {
  5. @Autowired
  6. private PrototypeBeanB prototypeBeanB;
  7. }
  1. @Data
  2. @Component
  3. @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  4. public class PrototypeBeanB {
  5. @Autowired
  6. private PrototypeBeanA prototypeBeanA;
  7. }
  1. @Test
  2. public void test3() {
  3. AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  4. applicationContext.register(PrototypeBeanA.class);
  5. applicationContext.register(PrototypeBeanB.class);
  6. applicationContext.refresh();
  7. applicationContext.getBean(PrototypeBeanA.class); // 此时必须要获取Spring管理的实例,因为现在scope="prototype" 只有请求获取的时候才会实例化对象
  8. }

运行结果如下:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'prototypeBeanA': Requested bean is currently in creation: Is there an unresolvable circular reference?
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:269)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1322)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1240)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:668)
 ... 89 more

源码分析

spring如何快速稳定解决循环依赖问题

org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

  1. ... ...
  2. // 判断是否在当前创建Bean池中
  3. if (isPrototypeCurrentlyInCreation(beanName)) {
  4. throw new BeanCurrentlyInCreationException(beanName);
  5. }
  6. ... ...

异常就是在上面的代码中抛出来的,那么beanName是什么时候添加至当前创建Bean池中的呢?

org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

  1. else if (mbd.isPrototype()) {
  2. // It's a prototype -> create a new instance.
  3. // prototype类型的bean的实例化
  4. Object prototypeInstance = null;
  5. try {
  6. beforePrototypeCreation(beanName);
  7. prototypeInstance = createBean(beanName, mbd, args);
  8. }
  9. finally {
  10. afterPrototypeCreation(beanName);
  11. }
  12. bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
  13. }

org.springframework.beans.factory.support.AbstractBeanFactory#beforePrototypeCreation

  1. protected void beforePrototypeCreation(String beanName) {
  2. // ThreadLocal
  3. Object curVal = this.prototypesCurrentlyInCreation.get();
  4. if (curVal == null) {
  5. this.prototypesCurrentlyInCreation.set(beanName);
  6. }
  7. else if (curVal instanceof String) {
  8. Set<String> beanNameSet = new HashSet<>(2);
  9. beanNameSet.add((String) curVal);
  10. beanNameSet.add(beanName);
  11. this.prototypesCurrentlyInCreation.set(beanNameSet);
  12. }
  13. else {
  14. Set<String> beanNameSet = (Set<String>) curVal;
  15. beanNameSet.add(beanName);
  16. }
  17. }

其根本原因就是Spring容器不会对prototype类型的bean进行缓存,因此无法提前利用三级缓存暴露一个代理对象。

循环依赖开关

可以通过allowCircularReferences来禁止循环依赖,这样的话,singleton bean的setter循环依赖也会报错。

  1. applicationContext.setAllowCircularReferences(false);

二级缓存可行?

缓存 说明
singletonObjects 第一级缓存,存放可用的成品Bean。
earlySingletonObjects 第二级缓存,存放半成品的Bean,半成品的Bean是已创建对象,但是未注入属性和初始化,用以解决循环依赖。
singletonFactories 第三级缓存,存的是Bean工厂对象,用来生成半成品的Bean并放入到二级缓存中,用以解决循环依赖。

理论上二级缓存时可行的,只需要将三级缓存中BeanFactory创建的对象提前放入二级缓存中,这样三级缓存就可以移除了。

那么spring中为什么还要使用三级缓存呢?如果要使用二级缓存解决循环依赖,意味着所有Bean在实例化后就要完成AOP代理,这样违背了Spring设计的原则,Spring在设计之初就是通过AnnotationAwareAspectJAutoProxyCreator这个后置处理器来在Bean生命周期的最后一步来完成AOP代理,而不是在实例化后就立马进行AOP代理。

到此这篇关于spring如何快速稳定解决循环依赖问题的文章就介绍到这了,更多相关spring循环依赖内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u022812849/article/details/114264411