Quick revision
The short answer
@Primary sets the default bean when several candidates of a type exist, and there should be only one primary per type. @Qualifier chooses a specific bean at the injection point by name or qualifier value, and a custom qualifier annotation does the same in a type-safe, readable way. @Fallback, added in Spring Framework 6.2, marks a bean that is used only when no other candidate exists. In a shared library I would avoid @Primary, because it can clash with the application's beans, and provide defaults with @ConditionalOnMissingBean in auto-configuration or with @Fallback.
What to remember
In this answer, we compared four ways to pick a bean among several candidates. @Primary is a bean-side default that applies to every injection point, so only one bean per type should be primary. @Qualifier chooses at the injection point by name or value, and custom qualifier annotations do the same with compiler checking and clearer intent. @Fallback, from Spring Framework 6.2, marks a bean that steps aside whenever another candidate exists. For shared libraries, @Primary is risky because it fights the application and other libraries, so defaults belong in auto-configuration with @ConditionalOnMissingBean, or behind @Fallback.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@Primary sets the default bean when several candidates of a type exist, and there should be only one primary per type. @Qualifier chooses a specific bean at the injection point by name or qualifier value, and a custom qualifier annotation does the same in a type-safe, readable way. @Fallback, added in Spring Framework 6.2, marks a bean that is used only when no other candidate exists. In a shared library I would avoid @Primary, because it can clash with the application's beans, and provide defaults with @ConditionalOnMissingBean in auto-configuration or with @Fallback.
Why this question is asked
These four annotations all answer one question, which bean should be injected when several exist, but they answer it from different places and with different strength. Mixing them up is how code bases end up with two @Primary beans and a startup failure after adding a library. Interviewers ask this comparison to see whether we can choose the right tool and, at this level, whether we think about who owns the decision: the application or the library. In this answer, we will recap the situation, look at each annotation with a small example, compare them side by side, and then apply them to a shared library.
1A Quick Recap of the Situation
Suppose two beans implement Notifier: EmailNotifier and SmsNotifier. Any injection point that asks for a Notifier has two candidates, and Spring needs a rule to pick one. The rule can live on the beans, saying which one is the default or which one should step aside, or at the injection point, saying which one this particular class wants. That distinction, bean side versus injection side, is the key to choosing between the annotations.
2@Primary: The Default on the Bean Side
@Primary marks one bean as the one to inject when the choice is otherwise ambiguous.
Notice that @Primary is global for the type: it affects every injection point in the context. That is convenient for a real application-wide default, and exactly why two primaries for one type make startup fail.
3@Qualifier and Custom Qualifiers: The Choice at the Injection Point
@Qualifier is placed where the dependency is injected and selects by name or by qualifier value. It is a filter, so it overrides @Primary at that point.
The string form is short but fragile: a typo or a renamed bean breaks it, and only at startup. A custom qualifier is checked by the compiler, reads as intent (this class wants the urgent notifier, not a bean called smsNotifier), and can carry attributes, such as @Channel("sms"), when one annotation should cover several beans.
4@Fallback: Stepping Aside on the Bean Side
@Fallback, new in Spring Framework 6.2, is the reverse of @Primary. Instead of naming the winner, it names a loser: a fallback bean is injected only if no other candidate is available.
| Annotation | Where | Effect | Good for |
|---|---|---|---|
@Primary | on the bean | wins whenever ambiguous | one application-wide default |
@Qualifier("name") | injection point | filters to that bean | a one-off specific choice |
| Custom qualifier | bean and injection point | filters by annotation | readable, type-safe choices |
@Fallback (6.2) | on the bean | used only if nothing else | library and framework defaults |
Notice the last line. @Fallback only removes the fallback from the contest; it does not decide among the application's own beans. That is the right behaviour for a default: it never takes a decision that belongs to someone else.
5What I Would Use in a Shared Library
A shared library does not own the application. If it marks its Notifier @Primary, it silently overrides the application's own choice, and if two libraries each mark an ObjectMapper or a RestClient.Builder as @Primary, the application cannot start until someone patches one of them. So libraries should avoid @Primary on any type the application might also define.
The established tool for library defaults is auto-configuration with @ConditionalOnMissingBean: the library's bean is not even registered if the application has defined its own. @Fallback is a good alternative when the bean must be registered anyway, for example because other library beans depend on it by name, or in plain Spring without auto-configuration. When the library needs its own private instance that the application should never receive, a qualifier on both sides keeps it separate.
6Follow-up questions
Can @Primary and @Fallback be combined?
They work at different steps: Spring first looks for a single primary bean and, if there is none, for a single bean that is not a fallback. In practice we rarely need both on one type; an application chooses its default with @Primary, and libraries use @Fallback, so they cooperate rather than collide.
Why is @ConditionalOnMissingBean preferred over @Fallback in Spring Boot starters?
Because the library's bean is never created when the application provides one, so there is no extra object, no extra connection pool and no surprise in a List<Notifier> injection. A @Fallback bean still exists and still appears when someone injects all Notifiers as a collection. @ConditionalOnMissingBean must be used in auto-configuration classes, which run after the application's own configuration, to be reliable.
Does @Qualifier work with @Bean methods?
Yes. A @Bean method can carry @Qualifier("urgent") or a custom qualifier annotation, and the produced bean gets that qualifier. The bean name, taken from the method name, also works as a qualifier value.
7Conclusion
In this answer, we compared four ways to pick a bean among several candidates. @Primary is a bean-side default that applies to every injection point, so only one bean per type should be primary. @Qualifier chooses at the injection point by name or value, and custom qualifier annotations do the same with compiler checking and clearer intent. @Fallback, from Spring Framework 6.2, marks a bean that steps aside whenever another candidate exists. For shared libraries, @Primary is risky because it fights the application and other libraries, so defaults belong in auto-configuration with @ConditionalOnMissingBean, or behind @Fallback.
Open with
The four annotations answer the same question, which candidate to inject, from different sides. @Primary sits on a bean and makes it the default whenever a type is ambiguous, across every injection point, so there must be only one primary per type; two primaries fail startup. @Qualifier sits at the injection point and filters the candidates by bean name or qualifier value, so it overrides @Primary for that one class.
Then, if they want more
A custom qualifier annotation, an annotation meta-annotated with @Qualifier such as @Urgent, does the same but is checked by the compiler, survives renames and reads as intent. @Fallback, added in Spring Framework 6.2, is the opposite of @Primary: the bean is used only when no other candidate exists, and it never decides among the application's own beans.
For a shared library, I would avoid @Primary on common types, because the library does not own the application: it silently overrides the application's choice and collides with other libraries' primaries.
The standard tool is auto-configuration with @ConditionalOnMissingBean, so the library's default is not registered at all when the application defines its own. @Fallback is a good option when the bean must exist anyway or outside Spring Boot. And for beans the library uses privately, I would put a qualifier on both sides so the application never receives them by accident.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare @Primary, @Qualifier, custom qualifier annotations and @Fallback. Which would...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/7a2f8e954c/