How can you prevent circular dependencies in Spring?

A cyclic dependency occurs when two or more Spring beans depend on each other directly or indirectly, creating a circular reference. This can lead to bean creation failures, especially when constructor injection is used. The best solution is to redesign the application to reduce tight coupling and establish clear dependency boundaries.

Key Points

Common Solutions

1. Refactor the Design

2. Use @Lazy

3. Introduce Interfaces

4. Use Setter Injection

5. Use Event-Based Communication

Example

Suppose OrderService depends on PaymentService and PaymentService depends on OrderService. Instead of directly referencing each other, create a separate TransactionService that coordinates both services.

@Service
public class OrderService {

    private final PaymentService paymentService;

    public OrderService(
            @Lazy PaymentService paymentService) {

        this.paymentService = paymentService;
    }
}

@Service
public class PaymentService {

    private final OrderService orderService;

    public PaymentService(
            @Lazy OrderService orderService) {

        this.orderService = orderService;
    }
}

Note

Using @Lazy resolves the issue technically, but redesigning the architecture is generally the preferred long-term solution.

Interview Tip

A concise interview answer is: Cyclic dependencies occur when two or more Spring beans depend on each other. The preferred solution is to refactor the design and remove tight coupling by introducing separate services or interfaces. If redesign is not feasible, Spring provides options such as @Lazy, setter injection, or event-driven communication to break the dependency cycle.

Last updated: 11 November 2026

Previous question: What is AOP, and what is its biggest disadvantage?

More Spring Framework - Level II interview questions

All Spring Framework - Level II questions