NoUniqueBeanDefinitionException class

The error thrown when several beans match a requested type and none of them wins.

A winner is the single @Primary bean, the only one not marked @Fallback, the one named like the parameter or the qualifier, or the one with the lowest jakarta @Priority. Fix it by marking one bean @Primary(), by selecting one with @Qualifier, or by injecting a List<T> when you want them all. It is a NoSuchBeanDefinitionException, so code that catches "no bean" also catches "no single bean".

// Two Greeter beans and no @Primary.
try {
  factory.getBean<Greeter>();
} on NoUniqueBeanDefinitionException catch (e) {
  log.warning(e.candidates); // [english, french]
}

Spring (Java):

try {
  factory.getBean(Greeter.class);
} catch (NoUniqueBeanDefinitionException e) {
  log.warn("{}", e.getBeanNamesFound());
}
Inheritance

Constructors

NoUniqueBeanDefinitionException(Type type, List<String> candidates, {String? qualifier, String? requiredBy, int? samePriority})
The beans named in candidates all match type; samePriority is set when they tie on @Priority.

Properties

beanName → String?
The requested bean name, or null for a lookup by type.
finalinherited
candidates → List<String>
The names of the matching beans (only the primary ones when several are primary).
final
cause → Object?
The error that caused this one, if any: what a constructor threw, or the container error of a nested bean.
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
message → String
A human-readable description of the problem.
no setteroverride
mostSpecificCause → Object
rootCause, or this exception when it has no cause. Spring: getMostSpecificCause().
no setterinherited
qualifier → String?
The requested qualifier, if any (lookups by type only).
finalinherited
requiredBy → String?
The bean being created when the lookup failed, if any.
finalinherited
rootCause → Object?
The innermost cause: cause followed through nested container errors, or null without a cause. Usually the error that really went wrong. Spring: getRootCause().
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
samePriority → int?
The @Priority value several candidates share as the highest, when that is why none could be chosen.
final
type → Type?
The requested type, or null for a lookup by name.
finalinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited