@Primary Annotation in Spring
Fast recall
Quick revision
Core answer
Knowledge of Spring's bean disambiguation strategy and the @Primary plus @Qualifier pairing for managing multiple beans of one type.
Putting @Qualifier at every injection point instead of using @Primary for the default, or thinking @Primary replaces @Qualifier or hides the other beans.
Explain the trade-off: @Primary covers the 90 percent case with zero annotation, while @Qualifier is explicit at the 10 percent of points needing the alternative — a maintainability decision.
Interview-ready guide
Interview answer
For example, CardGateway can be @Primary while BankGateway is another PaymentGateway bean. Injecting one PaymentGateway chooses CardGateway. @Qualifier("bank") chooses the bean carrying that qualifier. Injecting List
@Primary Annotation in Spring
Direct Answer
@Primary marks one bean as the preferred candidate when type-based injection finds several eligible beans for a single-valued dependency. @Qualifier narrows candidates by qualifier metadata; it is not simply an instruction that universally 'overrides' every other rule. After narrowing, a primary candidate may still help resolve the remaining set.
Code Example
@Configuration
public class DataSourceConfig {
@Bean
@Primary // default DataSource for most repositories
public DataSource mysqlDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/main");
return ds;
}
@Bean("analyticsDataSource")
public DataSource postgresDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:postgresql://localhost:5432/analytics");
return ds;
}
}
@Repository
public class OrderRepository {
private final DataSource dataSource;
// Gets mysqlDataSource because it is @Primary — no @Qualifier needed.
public OrderRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
}
@Repository
public class AnalyticsRepository {
private final DataSource dataSource;
// Explicitly requests the alternative — @Qualifier overrides @Primary.
public AnalyticsRepository(@Qualifier("analyticsDataSource") DataSource dataSource) {
this.dataSource = dataSource;
}
}
Common Mistakes & Pitfalls
- Using @Qualifier everywhere instead of @Primary: Freshers discover multiple beans cause errors, learn
@Qualifier, then repeat it on every injection point. Mark the common bean@Primaryand use@Qualifieronly for the alternative. - Thinking @Primary hides the other beans: It doesn't. You can still inject a non-primary bean with
@Qualifier.@Primaryis just the tie-breaker when no qualifier is given. - Marking two beans @Primary for one type: Spring still can't decide and throws at injection time. Only one bean per type may be
@Primary.
Check your understanding
Answer these without looking back at the article.
- Can you have multiple @Primary beans of the same type?
- How does @Primary interact with @Qualifier?
- When should you use @Primary vs @Profile?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.