spring-projects/spring-framework · error · NoSuchBeanDefinitionException

No qualifying bean of type '{}' available

Error message

No qualifying bean of type '{}' available

What it means

NoSuchBeanDefinitionException thrown by the default ObjectProvider.getObject() at line 89 when the provider's iterator is empty — i.e. no bean of the generic type exists. ObjectProvider.getObject() is strict: it demands exactly one bean, so an empty stream fails here (whereas getIfAvailable() would return null).

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/ObjectProvider.java:89

public interface ObjectProvider<T> extends ObjectFactory<T>, Iterable<T> {

	/**
	 * A predicate for unfiltered type matches, including non-default candidates
	 * but still excluding non-autowire candidates when used on injection points.
	 * @since 6.2.3
	 * @see #stream(Predicate)
	 * @see #orderedStream(Predicate)
	 * @see org.springframework.beans.factory.config.BeanDefinition#isAutowireCandidate()
	 * @see org.springframework.beans.factory.support.AbstractBeanDefinition#isDefaultCandidate()
	 */
	Predicate<Class<?>> UNFILTERED = (clazz -> true);


	@Override
	default T getObject() throws BeansException {
		Iterator<T> it = iterator();
		if (!it.hasNext()) {
			throw new NoSuchBeanDefinitionException(Object.class);
		}
		T result = it.next();
		if (it.hasNext()) {
			throw new NoUniqueBeanDefinitionException(Object.class, 2, "more than 1 matching bean");
		}
		return result;
	}

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * <p>Allows for specifying explicit construction arguments, along the
	 * lines of {@link BeanFactory#getBean(String, Object...)}.
	 * @param args arguments to use when creating a corresponding instance
	 * @return an instance of the bean
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Use getIfAvailable() / getIfAvailable(() -> default) / ifAvailable(consumer) for optional beans.
  2. Register the missing bean if it is mandatory.
  3. Use getObject() only when the bean is contractually guaranteed to exist.

Example fix

// before
OptionalFeature f = optionalFeatureProvider.getObject(); // throws if absent

// after
OptionalFeature f = optionalFeatureProvider.getIfAvailable(() -> defaultFeature);
Defensive patterns

Strategy: fallback

Validate before calling

// Prefer the null-safe accessor
if (provider.getIfAvailable() == null) {
    // bean genuinely absent; handle gracefully
}

Type guard

static <T> boolean isPresent(ObjectProvider<T> p) {
    return p.getIfAvailable() != null;
}

Prevention

When it happens

Trigger: Calling objectProvider.getObject() (directly or via getIfAvailable fallback path that rethrows) when no candidate of type T is registered. Common with ObjectProvider<T> injected as a lazy lookup handle.

Common situations: An optional feature bean that is conditionally absent; cross-module injection where the providing module is not on the classpath/config; calling getObject() instead of the null-safe getIfAvailable().

Related errors


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