描述:
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