一、异常出现场景以及异常信息

场景:SpringBoot中自定义DataSource数据源

异常信息:

-- ::30.807 ERROR  --- [           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: userServiceImpl (field com.zbq.springbootdemo.dao.UserDao com.zbq.springbootdemo.service.UserServiceImpl.userDao)

userDao defined in file [/Users/zhangboqing/Software/MyGithub/spring-boot-demo/target/classes/com/zbq/springbootdemo/dao/UserDao.class]

sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
| dynamicDataSource defined in class path resource [com/zbq/springbootdemo/config/multidatasource/DataSourceConfig.class]
↑ ↓
| primary defined in class path resource [com/zbq/springbootdemo/config/multidatasource/DataSourceConfig.class]
↑ ↓
| org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker

二、解决方案

排除Spring Boot数据源自动配置类

// 自定义数据源一定要排除SpringBoot自动配置数据源,不然会出现循环引用的问题,The dependencies of some of the beans in the application context form a cycle
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
05-11 02:10