@Primary tells Spring which bean to inject when several beans of the same type are candidates for a single injection point. Without it, Spring throws a NoUniqueBeanDefinitionException at startup because it cannot choose. With it, the marked bean becomes the default, and every other candidate stays available for code that asks for it explicitly.
@Primary is a default, not a hard rule. A more specific signal at the injection point, such as @Qualifier or a bean name, always wins over it.
Dependency injection in Spring resolves a dependency by type first. If one bean matches, it is injected. If two or more match, Spring needs a tie-breaker. The following setup is ambiguous:
Injecting NotificationSender into any class now fails with an error similar to this:
For a single-valued injection point, Spring applies these steps in order:
@Qualifier is present, keep only the candidates that match it.@Primary, use it.@Priority (javax or jakarta), use the highest priority.NoUniqueBeanDefinitionException.This order explains why @Primary is a good default but never overrides an explicit @Qualifier.
You can place @Primary on a stereotype-annotated class or on a @Bean method.
@Bean methodIn XML configuration the equivalent is the primary="true" attribute on the bean element.
With @Primary in place, a normal injection receives the primary bean. Constructor injection is the recommended style, and a class with a single constructor does not need @Autowired.
OrderService receives EmailSender even though SmsSender also implements the interface.
Use @Primary for the common case and @Qualifier for the exception. The qualifier overrides the default at that one injection point:
By default a component's bean name is the decapitalised class name, so SmsSender is registered as smsSender. If you inject all implementations with List<NotificationSender> or Map<String, NotificationSender>, @Primary has no effect: you receive every bean.
| Mechanism | Scope | Best used for |
|---|---|---|
@Primary | Global default for a type | The implementation most classes should use |
@Qualifier | One injection point | Choosing a specific bean explicitly |
@Priority | Ordering and fallback selection | Ranking candidates, mainly in Jakarta-style code |
| Bean name match | One injection point | Quick selection that depends on a parameter name |
@Primary beans of the same type cause a NoUniqueBeanDefinitionException that mentions more than one primary bean.@Primary does not remove other beans. They are still created and can be injected by qualifier or by collection.@Primary to hide a design problem leads to surprises. If different consumers need different implementations, qualifiers or separate interfaces express the intent better.@Fallback, which is the opposite: it is used only when no other candidate exists.@Primary test bean declared in a @TestConfiguration is a simple way to replace a production bean for the whole test context.Spring cannot decide and throws a NoUniqueBeanDefinitionException when the type is injected. Keep exactly one primary bean per type in a given context.
Yes. A @Qualifier at the injection point narrows the candidates before @Primary is considered, so the qualified bean is injected even if another bean is primary.
Not on its own. @Primary only has an effect on beans that Spring registers, so the class must be a component (@Component, @Service, and so on) or be returned from a @Bean method.
No. @Autowired(required = false) allows the injection to be skipped when no bean exists. @Primary resolves ambiguity when more than one bean exists.
@Primary makes one bean the default candidate when several beans of the same type exist. Put it on a component class or a @Bean method, keep it to one bean per type, and use @Qualifier at the injection points that need a different implementation. Qualifiers win over @Primary, and collection injection ignores it.