NoSuchBeanDefinitionException in Spring
Fast recall
Quick revision
Core answer
Practical debugging of one of the most common Spring startup failures, plus understanding of the fail-fast principle behind it.
Not checking the root cause systematically — it's usually a missing @Component, a package outside the component scan, a type mismatch, or a bean in an inactive profile.
Walk through the checklist (stereotype annotation, scan coverage, exact type, active profile) and mention enabling debug mode to read the conditions report instead of guessing.
Interview-ready guide
Interview answer
A missing dependency is detected while Spring builds the context, which is safer than leaving a required field null until traffic arrives. For example, PaymentService may require PaymentGateway, but the gateway implementation may sit outside component scanning or behind an inactive profile. The startup error names the failed bean and injection point. Follow the cause chain to the missing type, then verify registration and conditional rules. required=false can suppress failure, but then code must correctly handle absence; it should not be used to make a broken required configuration appear healthy.
NoSuchBeanDefinitionException in Spring
Direct Answer
If a required autowired dependency has no eligible bean, Spring cannot create the consuming bean and application-context startup fails. The top-level error is often UnsatisfiedDependencyException, with NoSuchBeanDefinitionException as a cause. Optional dependencies can be represented with Optional, ObjectProvider, or required=false, but making a core dependency optional usually hides a configuration problem.
Common Mistakes & Pitfalls
- Forgetting @Component or @Service: The most common cause. If the target class isn't annotated (or returned from a
@Beanmethod), Spring never creates a bean, so injection fails. - Wrong component scan package: If a class is in
com.example.databut@SpringBootApplicationlives incom.example.web, Spring won't scan the data package. Make sure your main class sits above all bean packages. - Mismatched types: If you
@Autowired CustomerRepositorybut only a different type is registered, Spring won't match them. Inject the type that actually exists. - Profile mismatch: A bean marked
@Profile("production")doesn't exist when you run with thedevprofile active.
Check your understanding
Answer these without looking back at the article.
- How do you debug NoSuchBeanDefinitionException?
- What is @Autowired(required=false)?
- What is NoUniqueBeanDefinitionException?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.