SpringBoot启动报错:Requested bean is currently in creation: Is there an unresolvable circular reference?
SpringBoot2.0+MyBatis+MySql的多数据源配置报错,报错如下:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dynamicDataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:339)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfUnique(DefaultListableBeanFactory.java:1905)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.getDataSourceInitializer(DataSourceInitializerInvoker.java:96)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:60)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
... 115 more
初步估计出现报错的原因是:出现了两个对数据库数据源的配置(项目配置了多数据源配置),一个是spring(或者是MyBatis)的数据源配置,另一个是我们自己的写的对多数据源的的管理配置。而Spring在项目启动的时候,在Bean容器里面出现了spring自动注入的一个管理数据源的Bean,还有一个就是我们自己项目中写的对数据源管理的Bean,所以我们在Mapper中获取Bean的时候不知道去获取哪个Bean或者重复获取Bean,就会造成上面的报错信息出现。(以上是自己的一些理解,不知道对不对,有大神知道具体较为详细的细节可以解释一下具体原因)
解决方案:在启动类添加
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
该注解的作用是取消自动注入数据源的配置(取消数据库配置)。