Spring Boot 2.1 bean overriding vs @Primary
Codemia Knowledge Hub ·
Bean overriding and @Primary solve different problems. Overriding replaces one bean definition with another bean of the same name. @Primary keeps every bean and tells Spring which one to prefer when several beans of the same type can be injected.
Bean overriding replaces a definition
Overriding is about bean definitions, not injection preference. If two beans are registered with the same name, one can replace the other only if overriding is allowed.
In Spring Boot 2.1, bean definition overriding is disabled by default, so the application usually fails fast instead of silently swapping one bean for another. To allow it:
With the property set, a later definition with the same name replaces the earlier one. Without it, Spring Boot 2.1 usually throws an error at startup.
@Primary picks a default for injection
@Primary replaces nothing. It only resolves ambiguity when multiple beans of the same type are available for autowiring.
Any bean that injects MessageService gets smsService by default because it is marked @Primary. Both beans still exist in the container.
In short:
- bean overriding decides whether one named bean definition can replace another
@Primarydecides which bean to inject when multiple beans of the same type are valid
Both affect "which bean gets used", but they act at different stages of the container lifecycle.
When @Primary is the fix
If both emailService and smsService exist and neither is primary, this injection is ambiguous unless you use @Qualifier. Marking one as @Primary resolves it.
When overriding is the issue
If a library defines a bean named objectMapper and your application defines one with the exact same name, @Primary does not help. That is a bean-definition conflict, not a type-selection problem. Your options are:
- rename one of the beans
- allow bean overriding explicitly
- use another customization hook provided by the library
Adding @Primary would leave both definitions in place and the duplicate-name startup error would remain.
Choosing the right tool
The most common mistake is reaching for @Primary when the real problem is duplicate bean names. It only helps during injection and cannot make one definition replace another.
The opposite mistake is enabling overriding as a quick fix for unclear configuration. Silent replacement can hide errors, which is one reason Spring Boot 2.1 turned it off by default. Prefer explicit configuration.
@Qualifier is often clearer when there is no single sensible default. @Primary fits best when one bean should usually win and the others are special-case alternatives.
Related reading
- Spring boot 2.4.0 The type HandlerInterceptorAdapter is deprecated
- Spring Boot 2.4.2 - DNS Resolution Problem at start on Apple M1
- Spring Boot 2.4.2 and Thymeleaf 3.0.12 - access static methods
- Spring Boot 2.5.0 generates plain.jar file. Can I remove it?
- Spring Boot 2.6.0 / Spring fox 3 - Failed to start bean 'documentationPluginsBootstrapper
- Spring Boot 2.6 regression How can I fix Keycloak circular dependency in adapter?
- Spring Boot 2 - Actuator Metrics Endpoint not working
- Spring Boot 2 - Change Jar Name
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.