Spring / Spring7 Intermediate to Advanced Interview questions
How do you troubleshoot a NoSuchBeanDefinitionException in a Spring application?
The exception means Spring searched the context for a bean of the requested type (or name) and found none, so troubleshooting is really a process of figuring out why the bean never got registered.
- Check that the class actually carries a stereotype annotation (
@Component,@Service, etc.) or is registered via an explicit@Beanmethod - a class with none of these is just a plain object as far as Spring is concerned. - Verify the class sits inside the component-scanned package tree; a class outside the base package (or an unusually structured multi-module project) is invisible to
@ComponentScanunless the base package is widened or the class is explicitly imported. - Check for a
@Conditional,@ConditionalOnProperty, or@Profileguarding the bean that isn't currently satisfied - run with--debugto print the auto-configuration/condition evaluation report and see exactly which conditions passed or failed. - If the lookup was by name (via
@QualifierorgetBean("name")), double-check for a typo against the actual registered bean name, which defaults to the class name with a lowercase first letter, or the@Beanmethod name.
More Related questions...