What is a circular dependency in Spring?

A circular dependency occurs when two or more Spring beans depend on each other directly or indirectly during object creation. Because each bean requires the other to be instantiated first, Spring cannot complete the dependency injection process, which may result in application startup failure.

Key Points

Example

Direct Circular Dependency

Bean A → Depends on Bean B

Bean B → Depends on Bean A

Spring cannot determine which bean should be created first.

Indirect Circular Dependency

Bean A → Bean B

Bean B → Bean C

Bean C → Bean A

This also creates a dependency cycle.

@Service
public class OrderService {

    private final PaymentService paymentService;

    public OrderService(
            PaymentService paymentService) {

        this.paymentService =
                paymentService;
    }
}

@Service
public class PaymentService {

    private final OrderService orderService;

    public PaymentService(
            OrderService orderService) {

        this.orderService =
                orderService;
    }
}

Result

BeanCreationException

Circular reference involving

OrderService and PaymentService

How to Resolve It

Real-World Example

In an e-commerce application

This creates a circular dependency.

A better design is

This removes the dependency cycle completely.

Best Practice

Avoid circular dependencies whenever possible. If they appear, they often indicate a design issue and can usually be eliminated through proper separation of responsibilities.

Interview Tip

A concise interview answer is: A circular dependency occurs when two or more Spring beans depend on each other for creation. For example, Bean A depends on Bean B and Bean B depends on Bean A. This can prevent Spring from creating the beans, especially when using constructor injection. The preferred solution is to redesign the application to remove the dependency cycle, although @Lazy and setter injection can be used as temporary fixes.

Last updated: 11 November 2026

Previous question: When should you use BeanFactory, and when should you use ApplicationContext?

Next question: What are the different ways to resolve circular dependencies in Spring Boot?

More Spring Framework - Level II interview questions

All Spring Framework - Level II questions