JAVAWarningFramework ErrorApril 30, 2026

Framework Error

Failed to autowire bean 'userService' of type [com.example.UserService] due to a circular dependency detected in the application context

What This Error Means

This error occurs when there is a circular dependency between two or more beans in the Spring IoC container. A bean is a managed object in the Spring framework, and when one bean depends on another, it's considered a dependency. However, when two beans depend on each other, it creates a circular dependency, which is not allowed in the Spring framework.

Why It Happens

Circular dependencies in the Spring framework typically occur when there is a mix of autowiring and constructor-based injection. For example, if a service class depends on another service class, and that service class also depends on the first service class, it creates a circular dependency. This can also happen when using @Autowired or @Inject to inject dependencies into beans.

How to Fix It

  1. 1To fix a circular dependency error, you can use the @Lazy annotation on one of the beans to delay the injection of the dependency. You can also use the @DependsOn annotation to specify the order of bean initialization. Alternatively, you can refactor your code to eliminate the circular dependency altogether.

Example Code Solution

❌ Before (problematic code)
Java
@Service
public class UserService {
    @Autowired
    private OrderService orderService;
}

@Service
public class OrderService {
    @Autowired
    private UserService userService;
}
✅ After (fixed code)
Java
@Service
public class UserService {
    @Autowired
    @Lazy
    private OrderService orderService;
}

Fix for Failed to autowire bean 'userService' of type [com.example.UserService] due to a circular dependency detected in the application context

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error