spring-projects/spring-framework · error · NoSuchBeanDefinitionException

No qualifying bean of type '{}' available

Error message

No qualifying bean of type '{}' available

What it means

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.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java:552

	/**
	 * Extract a unique bean for the given type from the given Map of matching beans.
	 * @param type the type of bean to match
	 * @param matchingBeans all matching beans found
	 * @return the unique bean instance
	 * @throws NoSuchBeanDefinitionException if no bean of the given type was found
	 * @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
	 */
	private static <T> T uniqueBean(Class<T> type, Map<String, T> matchingBeans) {
		int count = matchingBeans.size();
		if (count == 1) {
			return matchingBeans.values().iterator().next();
		}
		else if (count > 1) {
			throw new NoUniqueBeanDefinitionException(type, matchingBeans.keySet());
		}
		else {
			throw new NoSuchBeanDefinitionException(type);
		}
	}

}

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Annotate the target class as a stereotype (@Component/@Service/...) or declare an @Bean method.
  2. Fix @ComponentScan / @SpringBootApplication scan base packages so the class is picked up.
  3. Verify @ConditionalOn* predicates and the backing properties are satisfied.
  4. If absence is legitimate, use ObjectProvider / Optional injection or getIfAvailable().

Example fix

// before
public class UserRepo { ... } // never registered
// getBean(UserRepo.class) -> NoSuchBeanDefinitionException

// after
@Repository
public class UserRepo { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Check existence before mandatory lookup
if (!beanFactory.containsBean(MyRepo.class) &&
    beanFactory.getBeanNamesForType(MyRepo.class).length == 0) {
    throw new IllegalStateException("MyRepo bean missing; check component scan");
}

Type guard

static boolean beanExists(ListableBeanFactory bf, Class<?> t) {
    return bf.getBeanNamesForType(t).length > 0;
}

Try / catch

try {
    MyRepo r = bf.getBean(MyRepo.class);
} catch (NoSuchBeanDefinitionException ex) {
    r = fallbackRepo; // or throw a clearer domain error
}

Prevention

When it happens

Trigger: beanFactory.getBean(MyRepo.class) when no bean implements MyRepo — because the class is not annotated, component scanning excludes it, or @Conditional filtered it out.

Common situations: Forgetting @Component/@Repository; @ComponentScan basePackages wrong; @ConditionalOnProperty gating the bean with a missing property; requesting a bean from the wrong (child vs parent) context.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/a152f280fbe860b5. Report an issue: GitHub.