What is @Primary Annotation in Spring?
The @Primary annotation in Spring is used to define a default bean when multiple beans of the same type are available for Dependency Injection.
It tells the Spring IoC (Inversion of Control) container:
"Use this bean by default unless another bean is specifically selected using @Qualifier."
In simple terms:
- @Primary marks the preferred bean
- Spring injects it automatically by default
- It resolves ambiguity between multiple beans
- It works together with @Autowired
The @Primary annotation is widely used in:
- Spring Framework
- Spring Boot
- Microservices Architecture
- Enterprise Applications
- Cloud-Native Systems
Simple Definition
The @Primary annotation is used to specify the default bean among multiple beans of the same type.
Why @Primary Annotation is Important
In enterprise applications:
- Multiple implementations of the same interface may exist
- Spring may become confused during Dependency Injection
- Ambiguity errors can occur
The @Primary annotation helps Spring automatically select the preferred bean.
Real-Time Banking Example
Consider a banking application supporting:
- Email Notifications
- SMS Notifications
- Push Notifications
All implementations use the same interface:
NotificationService
One implementation can be marked as default using:
@Primary
Spring injects it automatically unless another bean is explicitly requested.
Problem Without @Primary
Interface
public interface NotificationService {
void sendNotification();
}
Implementation 1
@Service
public class EmailNotificationService
implements NotificationService {
}
Implementation 2
@Service
public class SmsNotificationService
implements NotificationService {
}
Autowired Injection Problem
@Service
public class PaymentService {
@Autowired
private NotificationService
notificationService;
}
Spring throws:
NoUniqueBeanDefinitionException
because multiple beans match the same type.
Using @Primary to Solve the Problem
@Service
@Primary
public class EmailNotificationService
implements NotificationService {
}
@Service
public class SmsNotificationService
implements NotificationService {
}
Now Spring automatically injects:
EmailNotificationService
How @Primary Works Internally
Application Starts
|
Spring Scans Beans
|
Multiple Beans Found
|
@Primary Bean Identified
|
Default Bean Selected
|
Dependency Injected
Basic @Primary Example
@Service
@Primary
public class EmailNotificationService
implements NotificationService {
public void sendNotification() {
System.out.println("Email Sent");
}
}
@Service
public class SmsNotificationService
implements NotificationService {
public void sendNotification() {
System.out.println("SMS Sent");
}
}
@Service
public class PaymentService {
@Autowired
private NotificationService
notificationService;
}
Spring injects EmailNotificationService automatically.
@Primary with @Bean Annotation
The @Primary annotation can also be used with:
@Bean
Example
@Configuration
public class AppConfig {
@Bean
@Primary
public NotificationService emailService() {
return new EmailNotificationService();
}
@Bean
public NotificationService smsService() {
return new SmsNotificationService();
}
}
@Primary vs @Qualifier
| Feature | @Primary | @Qualifier |
|---|---|---|
| Purpose | Default Bean Selection | Specific Bean Selection |
| Usage | Bean Definition | Injection Point |
| Behavior | Automatic Preference | Explicit Selection |
| Flexibility | Default Choice | Fine-Grained Control |
Using @Qualifier Overrides @Primary
Even if a bean is marked with:
@Primary
Spring uses the bean specified with:
@Qualifier
Example
@Service
@Primary
public class EmailNotificationService
implements NotificationService {
}
@Service
public class SmsNotificationService
implements NotificationService {
}
@Autowired
@Qualifier("smsNotificationService")
private NotificationService
notificationService;
Spring injects SmsNotificationService instead of the primary bean.
When to Use @Primary
- One implementation is commonly used
- Default business logic is required
- Large enterprise systems need default behavior
- Microservices require fallback implementations
Advantages of @Primary Annotation
- Resolves ambiguity automatically
- Reduces repetitive @Qualifier usage
- Provides default bean selection
- Improves maintainability
- Supports scalable architecture
1. Automatic Bean Selection
Spring automatically selects the preferred bean.
2. Cleaner Code
Developers do not need to use:
@Qualifier
everywhere.
3. Better Maintainability
Default implementations are centralized and easier to manage.
Disadvantages of @Primary Annotation
- Less explicit than @Qualifier
- May create confusion in large applications
- Not suitable when exact bean control is required
@Primary in Spring Boot
Spring Boot heavily uses:
@Primary
for:
- Database configurations
- REST clients
- Security providers
- Messaging systems
- Cloud integrations
Spring Boot DataSource Example
@Bean
@Primary
public DataSource primaryDataSource() {
return new HikariDataSource();
}
Spring uses this datasource by default.
@Primary in Microservices
Modern microservices architectures frequently use:
@Primary
because:
- Multiple implementations may exist
- Fallback services are common
- Cloud-specific implementations differ
Microservices Banking Example
@Service
@Primary
public class KafkaMessageService
implements MessageService {
}
@Service
public class RabbitMqMessageService
implements MessageService {
}
Kafka implementation becomes the default messaging service.
Common Spring Annotations Related to @Primary
- @Primary
- @Autowired
- @Qualifier
- @Bean
- @Service
- @Component
Real-World Enterprise Use Cases
- Default payment gateways
- Primary database selection
- Messaging platform configuration
- Notification systems
- Cloud provider integrations
Common Interview Questions
What is @Primary annotation in Spring?
The @Primary annotation is used to define the default bean when multiple beans of the same type exist.
What is the difference between @Primary and @Qualifier?
@Primary defines the default bean, while @Qualifier explicitly selects a specific bean.
Can @Qualifier override @Primary?
Yes. @Qualifier takes higher priority than @Primary.
Professional Interview Answer
The @Primary annotation in Spring is used to specify the default bean among multiple beans of the same type during Dependency Injection. When Spring encounters multiple matching beans, it automatically injects the bean marked with @Primary unless a specific bean is explicitly selected using @Qualifier. The @Primary annotation helps reduce ambiguity, simplifies configuration, and is heavily used in enterprise and microservices architectures.
Conclusion
The @Primary annotation is an important feature in Spring Framework and Spring Boot for managing multiple bean implementations efficiently.
It enables automatic default bean selection, reduces configuration complexity, and improves application maintainability.
Modern enterprise systems, cloud-native applications, banking platforms, REST APIs, and microservices architectures heavily rely on @Primary for scalable and flexible Dependency Injection management.
Understanding the @Primary annotation is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.