Skip to content

error: dependencies of some of the beans in the application context form a cycle: #1

@abhishek9773

Description

@abhishek9773

There is a circular dependency issue in the Spring Boot application, causing the application context initialization to fail.

Steps to Reproduce

  1. Clone the repository and navigate to the project directory.
  2. change branch main to -> issue branch
  3. Run the application using the command: ./gradlew bootRun
  4. Observe the error in the console.

Expected Behavior

The Spring Boot application should start without any dependency issues.

Actual Behavior

The application fails to start due to a circular dependency between PostController, PostServiceImpl, and UserServiceImpl.

Error Logs

2024-05-23T18:35:40.546+05:30  WARN 316150 --- [second] [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postConroller': Unsatisfied dependency expressed through field 'postService': Error creating bean with name 'postServiceImpl': Unsatisfied dependency expressed through field 'userService': Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'postService': Error creating bean with name 'postServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
2024-05-23T18:35:40.547+05:30  INFO 316150 --- [second] [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2024-05-23T18:35:40.550+05:30  INFO 316150 --- [second] [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-05-23T18:35:40.566+05:30  INFO 316150 --- [second] [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2024-05-23T18:35:40.570+05:30  INFO 316150 --- [second] [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2024-05-23T18:35:40.592+05:30  INFO 316150 --- [second] [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-05-23T18:35:40.626+05:30 ERROR 316150 --- [second] [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

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

   postConroller (field com.example.second.service.PostService com.example.second.controller.PostConroller.postService)
┌─────┐
|  postServiceImpl (field com.example.second.service.UserService com.example.second.service.PostServiceImpl.userService)
↑     ↓
|  userServiceImpl (field com.example.second.service.PostService com.example.second.service.UserServiceImpl.postService)
└─────┘


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.

Environment

  • Spring Boot Version: 3.2.5
  • Java Version: 18.0.2-ea
  • Build Tool: Gradle

console error:-

error_onConsole

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions