1. 问题描述1.1 启动Spring Boot项目时报循环依赖错误:The dependencies of some of the beans in the application context form a cycle┌─────┐ | userServiceImpl defined in file [D:\liyh\pro_new\diich-ecology\diich-biz\diich-biz-core\out\production\classes\com\diich\biz\service\core\UserServiceImpl.class] ↑ ↓ | favoriteServiceImpl defined in file [D:\liyh\pro_new\diich-ecology\diich-biz\diich-biz-core\out\production\classes\com\diich\biz\service\core\FavoriteServiceImpl.class] └─────┘1.2 SpringBoot中自定义DataSource数据源时报循环依赖错误:The dependencies of some of the beans in the application context form a cycleorg.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration ┌─────┐ | dataSource ↑ ↓ | scopedTarget.dataSource defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] ↑ ↓ | org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker └─────┘2. 问题原因以两个对象的相互依赖为例: Show
从异常信息可以看出,这是循环依赖问题。这种通常会出现在两个类注入Bean时互相使用了构造函数注入对方。Spring IoC容器在运行时检测到此循环引用就会报错。 在SpringBoot多模块项目中,两个项目互相引入对方的依赖,就会报这样的错误。 3. 解决方法1. 在实例变量上使用@Autowired注解,让Spring决定在合适的时机注入,而非在初始化类的时候就注入。 2. 用基于setter方法的依赖注入取代基于构造函数的依赖注入来解决循环依赖。 3. 排除Spring Boot数据源自动配置类 // 自定义数据源一定要排除SpringBoot自动配置数据源,不然会出现循环引用的问题,The dependencies of some of the beans in the application context form a cycle @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})4. 在 application.properties 文件中加入一行代码 spring.datasource.initialize=false5. 避免两个项目互相引入对方的依赖 List of articles
backgroundSpring Boot Version by 2.4.2 Upgrade to 2.6.8 , Then start Discovery PageHelper Report errors error message
solveLooking at the error message, it is obvious that there is a circular dependency , And found that Spring Boot 2.6 The above versions disable circular dependency by default . So there are two solutions
You can see pagehelper-spring-boot-starter The official new version solves this problem bug Of , So we upgrade pagehelper-spring-boot-starter Just go to the latest version How do you list all beans loaded in the application context?In Spring Boot, you can use appContext. getBeanDefinitionNames() to get all the beans loaded by the Spring container.
How do you get a bean in spring boot application context?Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.) In the Application , we create a bean, call its method and set up the Spring Boot application.
What is the latest version of spring boot?What is the latest Spring Boot version? The current stable version, as of July 2022, is Spring Boot 2.7.
|