Introduction
When developing applications using Spring Boot, you may encounter an error like this during application startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService'
or
UnsatisfiedDependencyException: Error creating bean with name
These errors occur when the Spring container fails to create or inject a bean into the application context.
In this article, we will understand:
• What BeanCreationException is
• Why Spring fails to create beans
• Common solutions to fix the problem
What is a Spring Bean?
In Spring, a bean is an object that is managed by the Spring IoC (Inversion of Control) container.
Spring automatically creates and manages beans when classes are annotated with annotations such as:
- @Component
- @Service
- @Repository
- @Controller
These beans are then injected into other classes using dependency injection.
Example Error
During application startup, you might see an error like this:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService':
Unsatisfied dependency expressed through field
This means Spring could not create or inject the required bean.
Cause 1: Missing @Component or @Service Annotation
Spring only creates beans for classes that are registered in the application context.
If a class is missing annotations like @Component, @Service, or @Repository, Spring will not create the bean.
Example problem:
public class UserService {
public String getUser() {
return "John";
}
}
In this case, Spring does not recognize the class as a bean.
Fix
Add the @Service annotation:
@Service
public class UserService {
public String getUser() {
return "John";
}
}
Cause 2: Package Scanning Problem
Spring Boot automatically scans components starting from the package where the main application class is located.
If your beans are located outside that package structure, Spring may fail to detect them.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
If your service classes are placed in a different package not scanned by Spring, the beans will not be created.
Fix
Ensure that your components are placed under the same base package as your main Spring Boot application class.
Cause 3: Multiple Beans of Same Type
If multiple beans of the same type exist, Spring may not know which one to inject.
@Service
public class EmailService implements NotificationService {}
@Service
public class SmsService implements NotificationService {}
When injecting NotificationService, Spring finds multiple candidates and throws an error.
Fix
Use the @Qualifier annotation.
@Autowired
@Qualifier("emailService")
private NotificationService notificationService;
Cause 4: Circular Dependency
A circular dependency occurs when two beans depend on each other.
For example, Service A depends on Service B, and Service B depends on Service A.
Example:
@Service
public class ServiceA {
@Autowired
private ServiceB serviceB;
}
@Service
public class ServiceB {
@Autowired
private ServiceA serviceA;
}
This creates a circular dependency that prevents Spring from properly initializing the beans.
Fix
Use constructor injection or redesign the dependency structure to remove the circular reference.
Best Practices to Avoid Bean Creation Errors
To avoid BeanCreationException in Spring Boot applications:
• Always annotate service classes with @Service, @Component, or @Repository
• Keep your classes inside the base package scanned by Spring Boot
• Avoid circular dependencies between services
• Use @Qualifier when multiple beans of the same type exist
• Prefer constructor injection instead of field injection****
Conclusion
Bean creation errors are common when working with Spring Boot applications.
Most issues occur due to missing annotations, incorrect package scanning, multiple beans of the same type, or circular dependencies.
By understanding these common causes, developers can quickly diagnose and fix BeanCreationException errors during application startup.
Top comments (0)