Your Spring Boot application crashes during startup with a massive stack trace ending in BeanCreationException. You’re not alone—this is one of the most common Spring Boot errors developers encounter.
TLDR - Quick Fix
BeanCreationException means Spring can’t create one of your beans. The most common causes and fixes:
// ❌ PROBLEM: Missing dependency
@Service
public class UserService {
@Autowired
private EmailService emailService; // EmailService doesn't exist
}
// ✅ FIX: Create the missing bean
@Service
public class EmailService {
// Implementation here
}
Quick diagnostic steps:
- Read the full error message—it tells you which bean failed
- Check if the bean class has
@Component,@Service,@Repository, or@Controller - Verify all
@Autowireddependencies exist and are in component scan path - Look for circular dependencies between beans
- Check constructor parameters have valid beans
In this troubleshooting guide, we’ll walk through the five most common causes of BeanCreationException and how to fix each one systematically.
Understanding BeanCreationException
When Spring Boot starts up, it scans your code for classes annotated with @Component (or its specializations like @Service, @Repository, @Controller) and creates instances—called “beans”—that it manages. When something goes wrong during this process, you get a BeanCreationException.
The error typically looks like this:
Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.example.service.UserService' available
This stack trace actually contains everything you need to solve the problem, but it’s buried in verbose output. Let’s break down the common scenarios.
Diagnostic Step: Read the Error Carefully
Before we dive into solutions, here’s how to parse the error message:
- Which bean failed? →
Error creating bean with name 'userController' - What went wrong? →
Unsatisfied dependency - What’s missing? →
No qualifying bean of type 'com.example.service.UserService'
Armed with this info, you can jump straight to the right fix.
Cause #1: Missing Component Annotation
This is the #1 cause. You created a class but forgot to tell Spring it’s a bean.
The error:
No qualifying bean of type 'com.example.service.EmailService' available
The problem:
// ❌ This class exists but Spring doesn't know about it
package com.example.service;
public class EmailService {
public void sendEmail(String to, String subject) {
// Implementation
}
}
The fix:
// ✅ Add @Service annotation
package com.example.service;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
public void sendEmail(String to, String subject) {
// Implementation
}
}
When to use which annotation:
@Service- Business logic layer@Repository- Data access layer (DAOs)@Controller/@RestController- Web layer@Component- Generic Spring-managed component
They all do the same thing under the hood, but using the right one makes your code more readable.
Cause #2: Component Not in Scan Path
Even with the right annotation, Spring won’t find your bean if it’s outside the component scan path.
The problem:
src/
main/
java/
com/
example/
Application.java // @SpringBootApplication is here
controller/
UserController.java
org/
different/
service/
EmailService.java // ❌ Outside 'com.example' package!
By default, @SpringBootApplication only scans the package where it’s located and all sub-packages. The EmailService above is in a different root package (org.different), so Spring won’t find it.
Fix #1: Move the class
src/
main/
java/
com/
example/
Application.java
controller/
UserController.java
service/
EmailService.java // ✅ Now it's in com.example.service
Fix #2: Explicitly configure component scan
@SpringBootApplication
@ComponentScan(basePackages = {
"com.example", // Default package
"org.different" // Additional package
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Most of the time, Fix #1 is cleaner—keep all your code under one root package.
Cause #3: Circular Dependencies
Spring can’t create beans that depend on each other in a circle.
The error:
The dependencies of some of the beans in the application context form a cycle:
userService defined in file [UserService.class]
↓
orderService defined in file [OrderService.class]
↓
userService
The problem:
@Service
public class UserService {
@Autowired
private OrderService orderService; // UserService needs OrderService
}
@Service
public class OrderService {
@Autowired
private UserService userService; // OrderService needs UserService
}
Spring doesn’t know which one to create first.
Fix #1: Use @Lazy annotation
@Service
public class UserService {
@Autowired
@Lazy // Don't inject until first use
private OrderService orderService;
}
@Service
public class OrderService {
@Autowired
private UserService userService;
}
The @Lazy annotation tells Spring to inject a proxy instead of the actual bean. The real bean is only created when you first use it, breaking the circular dependency.
Fix #2: Refactor your design (better long-term)
Circular dependencies often indicate a design problem. Consider extracting shared functionality:
// New service with shared logic
@Service
public class NotificationService {
public void notifyUser(String userId, String message) {
// Common notification logic
}
}
@Service
public class UserService {
@Autowired
private NotificationService notificationService;
}
@Service
public class OrderService {
@Autowired
private NotificationService notificationService;
}
Now there’s no cycle—both services depend on NotificationService, but it doesn’t depend on them.
Cause #4: Constructor Injection Problems
When you use constructor injection (which is actually the recommended approach), you can run into issues if the dependencies aren’t available.
The error:
Parameter 0 of constructor in com.example.controller.UserController required a bean of type
'com.example.service.UserService' that could not be found.
The problem:
@RestController
public class UserController {
private final UserService userService;
// Spring tries to call this constructor but can't find UserService
public UserController(UserService userService) {
this.userService = userService;
}
}
The fix (if UserService is missing):
// Make sure UserService exists and is annotated
@Service
public class UserService {
// Implementation
}
Why constructor injection is better:
Here’s the evolution from field injection to constructor injection:
// ❌ Field injection - harder to test, hides dependencies
@RestController
public class UserController {
@Autowired
private UserService userService;
}
// ✅ Constructor injection - easier to test, makes dependencies explicit
@RestController
public class UserController {
private final UserService userService;
@Autowired // Optional in Spring 4.3+
public UserController(UserService userService) {
this.userService = userService;
}
}
// ✅✅ Constructor injection with Lombok - cleanest
@RestController
@RequiredArgsConstructor // Generates constructor automatically
public class UserController {
private final UserService userService;
}
Constructor injection makes your code more testable because you can easily create the controller with a mock service in your tests.
Cause #5: Multiple Beans of Same Type
Sometimes you have multiple implementations of the same interface, and Spring doesn’t know which one to use.
The error:
Field paymentService in com.example.controller.PaymentController required a single bean,
but 2 were found:
- creditCardPaymentService
- paypalPaymentService
The problem:
public interface PaymentService {
void processPayment(double amount);
}
@Service
public class CreditCardPaymentService implements PaymentService {
public void processPayment(double amount) { /* ... */ }
}
@Service
public class PayPalPaymentService implements PaymentService {
public void processPayment(double amount) { /* ... */ }
}
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService; // ❌ Which one??
}
Fix #1: Use @Qualifier
@RestController
public class PaymentController {
@Autowired
@Qualifier("creditCardPaymentService") // Specify which bean
private PaymentService paymentService;
}
Fix #2: Use @Primary
@Service
@Primary // This is the default choice
public class CreditCardPaymentService implements PaymentService {
public void processPayment(double amount) { /* ... */ }
}
@Service
public class PayPalPaymentService implements PaymentService {
public void processPayment(double amount) { /* ... */ }
}
Now Spring will use CreditCardPaymentService unless you explicitly specify otherwise with @Qualifier.
Fix #3: Inject all implementations
If you actually need both, you can inject a list:
@RestController
public class PaymentController {
@Autowired
private List<PaymentService> paymentServices; // Gets all implementations
public void processWithPreferred(String type, double amount) {
PaymentService service = paymentServices.stream()
.filter(s -> s.getClass().getSimpleName().toLowerCase().contains(type))
.findFirst()
.orElseThrow();
service.processPayment(amount);
}
}
Still Not Working? Check These Edge Cases
Missing dependencies in pom.xml/build.gradle
Some Spring features require specific dependencies. For example, @Transactional needs:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Conditional beans not being created
If you’re using @ConditionalOnProperty or similar annotations, the bean might not be created:
@Service
@ConditionalOnProperty(name = "feature.email.enabled", havingValue = "true")
public class EmailService {
// Only created if feature.email.enabled=true in application.properties
}
Check your application.properties or application.yml file.
Profile-specific beans
@Service
@Profile("production")
public class ProductionEmailService implements EmailService {
// Only available when 'production' profile is active
}
Make sure you’re running with the correct profile: --spring.profiles.active=production
Summary Checklist
When you hit a BeanCreationException, work through this checklist:
- ✅ Read the full error message to identify which bean failed
- ✅ Verify the bean class has
@Service,@Component,@Repository, or@Controller - ✅ Check the bean is in the component scan path (same package or sub-package as
@SpringBootApplication) - ✅ Look for circular dependencies in the stack trace
- ✅ If using constructor injection, ensure all parameters have valid beans
- ✅ If multiple beans of same type exist, use
@Qualifieror@Primary - ✅ Check for conditional annotations that might prevent bean creation
- ✅ Verify all required dependencies are in your build file
Most BeanCreationException errors fall into one of the five categories we covered. The key is carefully reading the error message—Spring actually tells you exactly what’s wrong, you just need to know how to interpret it.
When debugging Spring Boot errors, use Debugly’s trace formatter to quickly parse and analyze Java stack traces. It highlights the important parts of the error, making it much easier to identify which bean failed and why. The formatted output helps you jump straight to the problematic line in your code instead of scrolling through hundreds of lines of stack trace.
Happy Spring debugging!