@Qualifier Annotation in Spring
Fast recall
Quick revision
Core answer
Ability to handle real cases where an interface has multiple implementations and one must be selected explicitly.
Not knowing the difference between @Qualifier and @Primary, or assuming bean names are always the class name with a lowercase first letter without configuring them.
Give the decision rule: @Primary for the default used almost everywhere, @Qualifier when different places genuinely need different implementations — and keep qualifier names in constants to avoid typos.
Interview-ready guide
Interview answer
Use @Qualifier when a type has meaningful variants, such as a live and sandbox PaymentGateway. Label the beans and put the matching qualifier on the constructor parameter. Spring filters the type-compatible candidates with that qualifier before choosing one. The qualifier string often equals the bean name, which makes it look like a pure name lookup, but custom qualifier annotations can express roles more safely. @Primary is useful for the usual default; @Qualifier is useful when one consumer deliberately needs another variant.
@Qualifier Annotation in Spring
Direct Answer
@Qualifier narrows the set of beans eligible for an injection point when type alone is not enough. Its value can match qualifier metadata and commonly matches a bean's qualifier or name, but it is better understood as a selection label rather than only as a bean-name lookup. @Primary supplies a default; @Qualifier requests a specific candidate.
Common Mistakes & Pitfalls
- Using @Qualifier without @Autowired: It isn't a standalone injection trigger — it only modifies
@Autowiredor constructor injection. Pair it with one of those. - Confusing @Qualifier with @Primary: Freshers reach for
@Qualifiereverywhere when@Primarywould be cleaner. If one implementation is the default most of the time, mark it@Primaryand qualify only the exceptions. - Hardcoding bean names everywhere: Scattering string literals like
"stripeGateway"makes code fragile — one rename and injection breaks. Keep names in constants or use a custom qualifier annotation.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @Qualifier and @Primary?
- What is NoUniqueBeanDefinitionException?
- Can you create a custom qualifier annotation?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.