In Spring, @Primary marks one bean as the preferred candidate when several beans of the same type could satisfy an injection point. Spring injects the primary bean by default, so you avoid a NoUniqueBeanDefinitionException without adding a qualifier everywhere.
The most common real-world use is an application with more than one DataSource, ObjectMapper, RestClient or TaskExecutor. This article uses a two-database setup in Spring Boot to show how @Primary behaves in practice.
Spring resolves dependencies by type. If you define two DataSource beans, a plain injection of DataSource has two matches and no way to choose. Spring Boot makes this worse in one specific way: its auto-configured DataSource is created only when you have not defined one yourself, so the moment you declare your own, the auto-configuration steps back and you must configure everything explicitly.
A second consequence is that other auto-configurations, such as JPA and JdbcTemplate, request a single DataSource. If two exist and none is primary, the application fails at startup.
The example below declares two data sources and makes the main one primary. It uses HikariDataSource, which is the default connection pool in Spring Boot.
The matching properties use jdbc-url because HikariDataSource binds that property name:
Any class that asks for a DataSource, including Spring Boot's own JdbcTemplate and JPA auto-configuration, receives mainDataSource.
A plain injection point gets the primary bean. To reach the other one, name it with @Qualifier:
The import javax.sql.DataSource lines are correct: DataSource is part of the JDK (java.sql module) and was not renamed during the move from Java EE to Jakarta EE.
Spring evaluates a qualifier before it considers @Primary. That gives you a predictable rule: @Qualifier is the exact request, @Primary is the default.
| Situation | Bean injected |
|---|---|
| No qualifier, one primary bean | The primary bean |
@Qualifier("reportingDataSource") | reportingDataSource, even though another bean is primary |
| No qualifier, no primary, parameter name matches a bean name | The bean with that name |
| No qualifier, no primary, no name match | NoUniqueBeanDefinitionException |
List<DataSource> or Map<String, DataSource> | All beans, regardless of @Primary |
ObjectMapper bean, mark it @Primary, and Spring MVC uses it for JSON.@TestConfiguration can declare a @Primary fake of a service so the whole test context uses it.@Primary and let special cases request theirs by qualifier.@Fallback, the inverse: a fallback bean is used only when no other candidate exists. It is handy for library defaults that application code can override without @Primary.Yes. Annotate the class with both @Component (or @Service, @Repository) and @Primary, and the scanned bean definition carries the primary flag.
Only at injection points that need a non-primary bean. Everywhere else the primary bean is injected automatically.
Check that the annotation is on exactly one bean of that type and that the configuration class is actually scanned. Also confirm the property prefix matches, because a missing URL fails later with a different error than the ambiguity one.
Yes. The resolution rule is the same for all injection styles, though constructor injection is preferred because it makes dependencies explicit and testable.
@Primary gives Spring a default when more than one bean of the same type exists. In a multi-datasource application it keeps Spring Boot's auto-configuration working against the main database, while @Qualifier selects the secondary one where needed. Keep a single primary bean per type and prefer explicit qualifiers when different consumers need different beans.