Spirng 循环依赖报错:Is there an unresolvable circular reference?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spirng 循环依赖报错:Is there an unresolvable circular reference?相关的知识,希望对你有一定的参考价值。
参考技术A 最近在项目中遇到了一次循环依赖报错的问题,虽然解决的很快,但是有些不明白的地方,特此记录。
在此我把 bean 的结构和 注入方式单独拎出来进行演示
点击进入 stack overflow 问题描述
BrokenComponent:
TolenComponent:
TubunComponent:
TwConfig:
DenpaComponent:
除了 tubunComponent 是构造器注入,其他的bean 都是set 注入。
分析bean依赖图可以发现确实是循环依赖的问题。
由于本人对spring bean 的加载机制不是很清晰,这次特意花了几天时间做了梳理。
解决了什么问题?
推荐博客:https://cloud.tencent.com/developer/article/1497692
作用:对通过 BeanFactory 创建的 bean 进行属性填充。
这是AbstractAutowireCapableBeanFactory#populateBean 方法中的某一段代码:
我打出了所有要进行轮询校验的 BeanPostProcessor
brokenComponent 中有两个bean:
一个是通过@Resource 注解注入的,一个是通过@Autowire 进行注入的,奉劝各位同学千万不要两种混用啊!!!
通过翻译及反复debug验证,这个类将对某个bean中所有被 @Resource 注解修饰的属性进行填充。
这个类将对某个bean中所有被 @Autowire@Value 注解修饰的属性进行填充。
1:无论使用@Resource 还是 @Resource,都不影响bean的初始化顺序。
2:@Resource修饰的属性 要优先于 @Autowire修饰的属性 进行初始化。
可以看到 tubunComponent 进行了两次 getSingleton,经过循环依赖检查的时候报错了。
1:Spring 容器先加载 brokenComponent 这个 bean。
2:brokenComponent依赖了tubunComponent(@Resource修饰),因此优先填充 tubunComponent,因此去初始化tubunComponent 这个bean,并将tubunComponent 存放在 DefaultSingletonBeanRegistry.singletonsCurrentlyInCreation 集合中。
3:tubunComponent依赖了depenComponent,因此去初始化depenComponent 这个bean,并将depenComponent 存放在 DefaultSingletonBeanRegistry.singletonsCurrentlyInCreation 集合中。
4:depenComponent依赖了tolenComponent,因此去初始化depenComponent 这个bean,并将tolenComponent 存放在 DefaultSingletonBeanRegistry.singletonsCurrentlyInCreation 集合中。
5:tolenComponent依赖了tubunComponent,因此去初始化tubunComponent 这个bean, 然而当将tubunComponent 存放在 DefaultSingletonBeanRegistry.singletonsCurrentlyInCreation 集合中时发现 tubunComponent 已经存在了,
从而判断这几个bean 产生了循环依赖并跑出异常。
beforeSingletonCreation(String beanName) 方法源码:
1:使用@Lazy 修饰。
2:这其实是一个操作不当造成的问题,因为spring的三级缓存已经解决了set注入导致的循环依赖,而这几个bean并不是全部都是使用构造器注入。
实验证明:
偏偏使用了会报错的一种组合......
springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?
2018-07-31 11:56:18.812 WARN 10316 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘adminController‘: Unsatisfied dependency expressed through field ‘adminUserService‘; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘adminUserService‘: Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘adminUserDao‘ defined in file [E:antdWorkspacepayadminv5 argetclassescompaycloudxdaoAdminUserDao.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory‘; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘sqlSessionFactory‘ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘dataSource‘ defined in class path resource [com/paycloudx/datasources/DynamicDataSourceConfig.class]: Unsatisfied dependency expressed through method ‘dataSource‘ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘firstDataSource‘ defined in class path resource [com/paycloudx/datasources/DynamicDataSourceConfig.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker‘: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘dataSource‘: Requested bean is currently in creation: Is there an unresolvable circular reference? 2018-07-31 11:56:18.825 INFO 10316 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2018-07-31 11:56:18.834 WARN 10316 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43) 2018-07-31 11:56:18.845 INFO 10316 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug‘ enabled. 2018-07-31 11:56:18.847 ERROR 10316 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: The dependencies of some of the beans in the application context form a cycle: adminController (field private com.paycloudx.service.AdminUserService com.paycloudx.controller.AdminController.adminUserService) ↓ adminUserService (field protected com.baomidou.mybatisplus.mapper.BaseMapper com.baomidou.mybatisplus.service.impl.ServiceImpl.baseMapper) ↓ adminUserDao defined in file [E:antdWorkspacepayadminv5 argetclassescompaycloudxdaoAdminUserDao.class] ↓ sqlSessionFactory defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class] ┌─────┐ | dataSource defined in class path resource [com/paycloudx/datasources/DynamicDataSourceConfig.class] ↑ ↓ | firstDataSource defined in class path resource [com/paycloudx/datasources/DynamicDataSourceConfig.class] ↑ ↓ | org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker └─────┘ Process finished with exit code 1
项目框架:
springboot + Mybatis + Mybatis Plus
配置了druid多数据源,刚搭建的时候启动正常,写好Controller,Service,Dao等代码后,启动报错。
百度不到对应的错了,问了公司大神,一下子就解决了,唉。
解决方案:
启动类注解:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
以上是关于Spirng 循环依赖报错:Is there an unresolvable circular reference?的主要内容,如果未能解决你的问题,请参考以下文章
springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?
springboot出现Requested bean is currently in creation: Is there an unresolvable circular reference?(代码
EF There is already an open DataReader associated with this Command
There is already an object named ‘#xxxx‘ in the database.
There is an error in invoking javac. A full JDK (not just JRE) is required
MyBatisBatchItemWriter Cannot change the ExecutorType when there is an existing transaction