Spring Boot
Bean Override
Primary Annotation
Spring Framework
Java Development

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:

properties
spring.main.allow-bean-definition-overriding=true

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.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Primary;
4
5@Configuration
6public class AppConfig {
7
8    @Bean
9    public MessageService emailService() {
10        return new EmailMessageService();
11    }
12
13    @Bean
14    @Primary
15    public MessageService smsService() {
16        return new SmsMessageService();
17    }
18}

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
  • @Primary decides 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

java
1import org.springframework.stereotype.Service;
2
3@Service
4public class NotificationRunner {
5    private final MessageService messageService;
6
7    public NotificationRunner(MessageService messageService) {
8        this.messageService = messageService;
9    }
10}

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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms