Spring Boot learning path Level 2 This level · 5 of 11
Medium java spring

What happens if two beans of the same type exist without @Qualifier?

The 30 second answer

Two beans of the same type don't automatically cause a failure. For a single dependency, Spring tries to choose one using rules such as @Primary, a matching injection-point name, @Priority and a unique default candidate. If ambiguity remains when that dependency is resolved, it throws NoUniqueBeanDefinitionException, usually failing startup for an eager singleton. Injecting a list or a bean-name-keyed map instead collects the matching beans, so having multiple candidates is expected rather than ambiguous.

Could you say this in an interview?

The tie-break logic is in one method:

// Step 1: check primary candidate
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
if (primaryCandidate != null) {
    return primaryCandidate;
}
// Step 2a: match bean name against declared dependency name
String dependencyName = descriptor.getDependencyName();
if (dependencyName != null) {
    for (String beanName : candidates.keySet()) {
        if (matchesBeanName(beanName, dependencyName)) {
            return beanName;
        }
    }
}
// Step 2b: match bean name against qualifier-suggested name
// Step 3: check highest priority candidate
// Step 4: pick unique default-candidate
// Step 5: pick directly registered dependency

DefaultListableBeanFactory.determineAutowireCandidate L2038-L2082 (shortened)

If the method returns null, the factory throws NoUniqueBeanDefinitionException (L2105).

Example:

@Bean PaymentGateway stripeGateway() { ... }
@Bean PaymentGateway paypalGateway() { ... }

@Service
class CheckoutService {
    CheckoutService(PaymentGateway gateway) { }        // fails: no tie-break
    // CheckoutService(PaymentGateway stripeGateway)  // works: name match
}

Ways to fix it:

  1. **@Primary** on the bean that should be the default.
  2. **@Qualifier("stripeGateway")** at the injection point. This is the clearest option.
  3. Name match: name the parameter like the bean. This works but is fragile: renaming a variable changes wiring. Constructor parameter names are only known if the code is compiled with -parameters; Spring Framework 6.1 removed the older bytecode-based fallback.
  4. Inject all: List<PaymentGateway> (ordered by @Order) or Map<String, PaymentGateway> keyed by bean name, then choose at runtime. Good for strategy patterns.
  5. **@Fallback** (Spring Framework 6.2+): marks a bean that is used only when no other candidate exists.

Interview trap: the error is NoUniqueBeanDefinitionException, not NoSuchBeanDefinitionException. Also, ObjectProvider<T>.getIfUnique() returns null instead of failing when there are several beans.

Editor's note: prefer @Qualifier or @Primary in shared code; relying on parameter names hides intent.

Sources (3)
  1. NoUniqueBeanDefinitionException Javadocdocs.spring.io
  2. Spring Framework reference: Fine-tuning autowiring with @Primary or @Fallbackdocs.spring.io
  3. Spring Framework reference: Fine-tuning with qualifiersdocs.spring.io
Previous
Next