{"record":{"id":"a152f280fbe860b5","repo":"spring-projects/spring-framework","slug":"no-qualifying-bean-of-type-available","errorCode":null,"errorMessage":"No qualifying bean of type '{}' available","messagePattern":"No qualifying bean of type '(.+?)' available","errorType":"exception","errorClass":"NoSuchBeanDefinitionException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java","lineNumber":552,"sourceCode":"\n\t/**\n\t * Extract a unique bean for the given type from the given Map of matching beans.\n\t * @param type the type of bean to match\n\t * @param matchingBeans all matching beans found\n\t * @return the unique bean instance\n\t * @throws NoSuchBeanDefinitionException if no bean of the given type was found\n\t * @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found\n\t */\n\tprivate static <T> T uniqueBean(Class<T> type, Map<String, T> matchingBeans) {\n\t\tint count = matchingBeans.size();\n\t\tif (count == 1) {\n\t\t\treturn matchingBeans.values().iterator().next();\n\t\t}\n\t\telse if (count > 1) {\n\t\t\tthrow new NoUniqueBeanDefinitionException(type, matchingBeans.keySet());\n\t\t}\n\t\telse {\n\t\t\tthrow new NoSuchBeanDefinitionException(type);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":534,"sourceCodeEnd":557,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java#L534-L557","documentation":"NoSuchBeanDefinitionException from BeanFactoryUtils.uniqueBean when zero candidates match the requested type. Means there is simply no bean of that type in the (possibly merged) factory. This helper is used by beanOfType overloads that require exactly one instance.","triggerScenarios":"beanFactory.getBean(MyRepo.class) when no bean implements MyRepo — because the class is not annotated, component scanning excludes it, or @Conditional filtered it out.","commonSituations":"Forgetting @Component/@Repository; @ComponentScan basePackages wrong; @ConditionalOnProperty gating the bean with a missing property; requesting a bean from the wrong (child vs parent) context.","solutions":["Annotate the target class as a stereotype (@Component/@Service/...) or declare an @Bean method.","Fix @ComponentScan / @SpringBootApplication scan base packages so the class is picked up.","Verify @ConditionalOn* predicates and the backing properties are satisfied.","If absence is legitimate, use ObjectProvider / Optional injection or getIfAvailable()."],"exampleFix":"// before\npublic class UserRepo { ... } // never registered\n// getBean(UserRepo.class) -> NoSuchBeanDefinitionException\n\n// after\n@Repository\npublic class UserRepo { ... }","handlingStrategy":"validation","validationCode":"// Check existence before mandatory lookup\nif (!beanFactory.containsBean(MyRepo.class) &&\n    beanFactory.getBeanNamesForType(MyRepo.class).length == 0) {\n    throw new IllegalStateException(\"MyRepo bean missing; check component scan\");\n}","typeGuard":"static boolean beanExists(ListableBeanFactory bf, Class<?> t) {\n    return bf.getBeanNamesForType(t).length > 0;\n}","tryCatchPattern":"try {\n    MyRepo r = bf.getBean(MyRepo.class);\n} catch (NoSuchBeanDefinitionException ex) {\n    r = fallbackRepo; // or throw a clearer domain error\n}","preventionTips":["Add the stereotype annotation (@Repository/@Service) to every bean.","Verify @ComponentScan basePackages cover the package.","For optional dependencies, inject ObjectProvider / Optional."],"tags":["dependency-injection","bean-resolution","no-such-bean"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}