Spring循环依赖 This is often the result of over-eager type matching - consider using 'getBeanNamesOfT...

结论:当A、B对象之间相互依赖,A早于B实例化,并且A对象使用了@Async注解或由BeanPostProcessor后置处理器返回了代理对象时,会产生以下报错:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a': Bean with name 'a' has been injected into other beans [b] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:585) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)

在springboot中,以上报错被捕捉,抛出的异常是:

The dependencies of some of the beans in the application context form a cycle


原因:我们知道,spring三级缓存一定程度上解决了循环依赖问题。A对象在实例化之后,属性赋值【opulateBean(beanName, mbd, instanceWrapper)】执行之前,将ObjectFactory添加至三级缓存中,从而使得在B对象实例化后的属性赋值过程中,能从三级缓存拿到ObjectFactory,调用getObject()方法拿到A的引用,B由此能顺利完成初始化并加入到IOC容器。此时A对象完成属性赋值之后,将会执行初始化【initializeBean(beanName, exposedObject, mbd)方法】,重点是@Async注解的处理正是在这地方完成的,其对应的后置处理器AsyncAnnotationBeanPostProcessor,在postProcessAfterInitialization方法中将返回代理对象,此代理对象与B中持有的A对象引用不同,导致了以上报错。


有以下解决办法:

    1)在A类上加@Lazy,保证A对象实例化晚于B对象;

    2)在A类上使用@DependsOn注解,保证A对象实例化晚于B对象(注意如果AB两个类相互DependsOn,也会形成循环依赖)

    3)设置 AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(true),不会报错但会导致B持有的A引用不是最终的代理对象;

    4)不使用@Async注解,通过自定义异步工具类发起异步线程;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章