Spring循环依赖问题解决

描述:

springboot自定义Hikari多数据源,打断点总是bean装载出现问题,

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'hikariMaster': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:714) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	... 47 common frames omitted

一步一步注释,直到到下面循环依赖路径。

***************************
APPLICATION FAILED TO START
***************************

Description:

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

   customsController defined in file [D:\OceanPaymentWorkSpace\op-gateway\target\classes\com\oceanpayment\opgateway\controller\CustomsController.class]
      ↓
   customsServiceImpl defined in file [D:\OceanPaymentWorkSpace\op-gateway\target\classes\com\oceanpayment\opgateway\service\impl\CustomsServiceImpl.class]
      ↓
   customsMapper defined in file [D:\OceanPaymentWorkSpace\op-gateway\target\classes\com\oceanpayment\opgateway\mapper\CustomsMapper.class]
      ↓
   sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
|  dynamicDataSource defined in class path resource [com/oceanpayment/opgateway/common/config/datasource/DynamicDataSourceConfiguration.class]
↑     ↓
|  hikariMaster defined in class path resource [com/oceanpayment/opgateway/common/config/datasource/HikariConfiguration.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘

 

解决:

1.

配置类加入:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

或启动类加入

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

2.

如果还是存在开始的错误堆栈报错,考虑分析bean之间的依赖关系,考虑 @Lazy 注解延迟创建依赖顶层的Bean

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值