When multiple beans of the same type exist in a Spring application context, @Autowired alone can't determine which one to inject, so @Qualifier is used alongside it to disambiguate by specifying the exact bean name.
Key Points: • Without disambiguation, Spring throws a NoUniqueBeanDefinitionException at startup when it finds more than one candidate bean. • @Qualifier("beanName") on the injection point tells Spring exactly which bean to wire in. • @Primary can mark one bean as the default choice, but @Qualifier takes precedence when both are present and conflict. • Mismatched or misspelled qualifier values fail silently into a startup error rather than a runtime surprise, since Spring validates wiring eagerly. • Mixing @Primary and @Qualifier inconsistently across a codebase can make it unclear which bean actually gets injected where, hurting readability.
Example: Two PaymentGateway implementations, StripeGateway and PaypalGateway, are both registered as beans; injecting with @Qualifier("stripeGateway") ensures the Stripe implementation is wired in at a specific injection point instead of an ambiguous default.
Code Example:
@Service
public class CheckoutService {
private final PaymentGateway paymentGateway;
public CheckoutService(@Qualifier("stripeGateway") PaymentGateway paymentGateway) {
this.paymentGateway = paymentGateway;
}
}Interview Tip: A concise interview answer is:
"When there are multiple beans of the same type, plain @Autowired throws a NoUniqueBeanDefinitionException, so I pair it with @Qualifier to name the exact bean I want. Mixing in @Primary as a default is fine, but if @Qualifier values are misspelled or inconsistent across the codebase, it becomes unclear which implementation actually gets wired in, so I keep qualifier names deliberate and consistent."