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

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 cycle

org.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. 问题原因

以两个对象的相互依赖为例:

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

从异常信息可以看出,这是循环依赖问题。这种通常会出现在两个类注入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=false

5. 避免两个项目互相引入对方的依赖

List of articles

    • background
    • error message
    • solve

Here is weihubeats, Feel good about the official account. Minor technique , The article first . Reject marketing number , Reject the title party

background

Spring Boot Version by 2.4.2 Upgrade to 2.6.8 , Then start Discovery PageHelper Report errors

error message

  • pagehelper-spring-boot-starter edition : 1.3.0
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 13.060 ERROR o.s.b.d.LoggingFailureAnalysisReporter:40 restartedMain *************************** APPLICATION FAILED TO START *************************** Description: The dependencies of some of the beans in the application context form a cycle: ┌──->──┐ | com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration └──<-──┘ Action: Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true. Process finished with exit code 0

solve

Looking 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

  1. Turn on circular dependency
spring.main.allow-circular-references = true

This is not recommended , After all, circular dependencies are disabled , Therefore, circular dependency is not recommended . Nor should our code have circular dependencies

  1. upgrade pagehelper-spring-boot-starter edition

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

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.