CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Spring Boot Circular Dependency Error

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Spring Boot Circular Dependency Error

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X

Table of Contents

What is a circular dependency?
How does Spring create a Bean?
Solution
Lazily Initialize a Bean
Via configuration file
Conclusion

What is a circular dependency?

The circular dependency error occurs when a bean type A is dependent on B and B is also dependent on A.

Example:

BeanA -> BeanB -> BeanA

BeanA

@Component
public class BeanA{
    @Autowired
    private BeanB beanB;
}

BeanB

@Component
public class BeanB{
    @Autowired
    private BeanA beanA;
}

How does Spring create a Bean?

Let’s take an example of the following scenario:

BeanA -> BeanB -> BeanC

In the above scenario, the spring first creates a bean of type BeanC and creates a bean of type BeanB then injects BeanC into Bean B and creates a bean type of BeanA then injects BeanB into BeanA. There is not any confusion while creating and injecting these beans.

But, let’s take an example of the following scenario:

BeanA -> BeanB -> BeanA

In this case, the spring can’t decide which dependency to create first. Because the bean of type BeanA is dependent on BeanB and BeanB is also dependent on BeanA.

This is why circular dependency occurs.

Solution

The best option is to redesign the code structure. So that we can avoid this type of issue. But still there needs to be a workaround apart from the redesign. Because there may be legacy code that might be already tested and redesigning this code may take a longer time to develop as well as test.

Hence, we can use the following solutions.

Lazily Initialize a Bean

To initialize bean lazily we can use @Lazy annotation from org.springframework.context.annotation package.

Example:

BeanA

@Component
public class BeanA{
    @Autowired
    private BeanB beanB;
}

BeanB

@Component
public class BeanB{
    @Autowired
    @Lazy
    private BeanA beanA;
}

The main disadvantages of this approach is, we need to define @Lazy annonation on every place where dependency is injected.

Via configuration file

Another and easiest solution is to tell Spring application to allow circular reference.

We can set following configuration to true either in application.properties or application.yml file.

Example:

application.properties

spring.main.allow-circular-references= true

application.yml file

spring:
  main:
    allow-circular-references: true

In this way, we can solve the circular dependency error in Spring.

Conclusion

In this post, we learned to solve the circular dependency error in spring by lazily initializing the beans.

Related Posts:

  • Most Frequently Asked Spring Boot Interview…
  • StackOverflowError in JPA Mapping @ManyToMany with…
  • Pagination and Sorting in Spring Boot
  • How to Implement Scheduled Tasks in Spring Boot: A…
  • How to Fix NoClassDefFoundError:…
  • Spring Boot Thread Pool Configuration: Optimizing…
Tags:javaspringspring-boot
Was this article helpful?
โ† Previous ArticleCreate User in MySQL
Next Article โ†’Logging MongoDB Queries in Java?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap