Quick revision
The short answer
We define both DataSource beans ourselves and annotate the primaryDs @Bean method with @Primary, so every injection point that asks for a plain DataSource, including Spring Boot's JPA and JDBC auto-configuration, gets primaryDs. Where we need the other one, we inject it with @Qualifier("reportingDs") DataSource. Because Spring Boot's DataSource auto-configuration backs off once we define our own DataSource, we configure both explicitly, and a custom qualifier annotation such as @Reporting avoids the string name.
What to remember
In this answer, we defined two DataSource beans with their own properties, marked primaryDs with @Primary so that every plain DataSource injection point, including Spring Boot's JPA and JDBC auto-configuration, gets it, and injected reportingDs with @Qualifier("reportingDs") where it is needed. Because Boot's DataSource auto-configuration backs off once we define our own, we configured both explicitly. Finally, a custom @Reporting qualifier annotation replaced the string name with something the compiler checks.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define both DataSource beans ourselves and annotate the primaryDs @Bean method with @Primary, so every injection point that asks for a plain DataSource, including Spring Boot's JPA and JDBC auto-configuration, gets primaryDs. Where we need the other one, we inject it with @Qualifier("reportingDs") DataSource. Because Spring Boot's DataSource auto-configuration backs off once we define our own DataSource, we configure both explicitly, and a custom qualifier annotation such as @Reporting avoids the string name.
Why this question is asked
Almost every growing service eventually needs a second database: a read replica for reports, a legacy database during a migration, or an analytics store. Interviewers ask this coding question because it combines three ideas in a few lines: choosing a default among beans of the same type, choosing a specific one at an injection point, and knowing how Spring Boot's auto-configuration reacts when we take over. In this answer, we will recap the problem with two beans of one type, write the configuration with properties, mark the default, inject the reporting DataSource, see why Boot backs off, and replace the string with a custom qualifier.
1A Quick Recap of the Problem
A DataSource is the object that hands out database connections, usually backed by a connection pool such as HikariCP. Spring Boot normally creates one for us from spring.datasource.* properties. If we define two beans of type DataSource, every injection point that simply asks for a DataSource now has two candidates, and Spring fails with NoUniqueBeanDefinitionException unless we tell it which one to use.
We need two tools for that. @Primary marks the bean to use by default whenever the choice is ambiguous. @Qualifier, placed at an injection point, asks for a specific bean and overrides the default there.
2Defining Both DataSources
Each DataSource gets its own properties prefix, bound with DataSourceProperties, the same class Boot uses internally, so we keep Boot's familiar property names and pool settings.
3Marking the Default With @Primary
Now the configuration class. The primary DataSource is marked @Primary; the reporting one is not.
Notice that the two DataSourceProperties beans are also of one type, so the primary one is marked @Primary too, and the reporting one is picked with a qualifier. The bean names come from the method names: primaryDs and reportingDs. Every plain DataSource injection point now gets primaryDs, and that includes Spring Boot's own auto-configuration for JPA, JdbcTemplate and Flyway, which ask for a single DataSource.
4Injecting the Reporting DataSource Where Needed
The reporting code asks for the second DataSource explicitly.
5 messages in order. Play them, or step through them one at a time.
The qualifier is a hard choice at that one injection point. Elsewhere @Primary keeps deciding, so the rest of the application did not need to change at all.
5Why Spring Boot Backs Off
Spring Boot's DataSourceAutoConfiguration creates a DataSource only when the application has not defined one; it is guarded by a condition equivalent to @ConditionalOnMissingBean(DataSource.class). As soon as we define primaryDs, Boot steps aside, and the spring.datasource.* properties no longer do anything unless our own beans read them. That is why we configure both DataSources explicitly, including the pool settings under the hikari sub-prefix, and why it is a good idea to delete the now unused spring.datasource.url lines, so nobody edits a property that has no effect.
JPA needs one more decision. Boot's JPA auto-configuration builds a single EntityManagerFactory on the primary DataSource. If reports only use JdbcTemplate, that is all we need. If the reporting database also needs JPA entities, we define a second LocalContainerEntityManagerFactoryBean and transaction manager for it, again with qualifiers.
6A Custom Qualifier Instead of a String
The string "reportingDs" is easy to mistype and breaks silently on a rename. A custom qualifier annotation, an annotation that is itself annotated with @Qualifier, gives the same effect with compiler checking.
Now a search for @Reporting shows every place that uses the replica, and the bean method can be renamed freely.
7Follow-up questions
Could we use @Bean(defaultCandidate = false) for the reporting DataSource instead?
Spring Framework 6.2 added defaultCandidate = false, which keeps a bean out of plain by-type injection unless the injection point names it with a qualifier. Marking reportingDs that way means only one default candidate remains, so even without @Primary plain DataSource injection gets the main one, and recent Spring Boot versions use this in their guidance for a second DataSource (check your version). @Primary still works everywhere and is the more familiar choice.
What happens to @Transactional with two DataSources?
Boot's transaction manager is created for the primary DataSource, so @Transactional methods open transactions on db-main. Work on the reporting DataSource is not covered by them. If reports need transactions, we define a second transaction manager bean and choose it with @Transactional("reportingTxManager"), or with a custom annotation that carries the transactionManager attribute.
Is @Primary on a library DataSource a good idea?
No. In application code, one @Primary per type is a clear, local decision. In a shared library it would override the application's own choice and clash with other libraries, so libraries should use conditions such as @ConditionalOnMissingBean or @Fallback instead.
8Conclusion
In this answer, we defined two DataSource beans with their own properties, marked primaryDs with @Primary so that every plain DataSource injection point, including Spring Boot's JPA and JDBC auto-configuration, gets it, and injected reportingDs with @Qualifier("reportingDs") where it is needed. Because Boot's DataSource auto-configuration backs off once we define our own, we configured both explicitly. Finally, a custom @Reporting qualifier annotation replaced the string name with something the compiler checks.
Open with
I would define both DataSources myself, each with its own properties prefix, for example app.datasource.primary and app.datasource.reporting, bound through DataSourceProperties so I keep Boot's familiar URL, username and Hikari pool settings.
Then, if they want more
The primaryDs @Bean method gets @Primary. That means any injection point that just asks for a DataSource, including Spring Boot's own JPA, JdbcTemplate and Flyway auto-configuration, gets primaryDs without any change. Where I need the replica, I inject it explicitly with @Qualifier of reportingDs on the constructor parameter; the qualifier overrides the primary at that one point.
It is important to know that Spring Boot's DataSourceAutoConfiguration only creates a DataSource when none is defined, so as soon as I define my own it backs off, and the spring.datasource properties stop having any effect. That is why I configure both explicitly and remove the old properties so nobody edits them by mistake.
If the reporting database also needs JPA or transactions, I add a second EntityManagerFactory or transaction manager with qualifiers, since Boot only builds them for the primary. Finally, I would replace the string with a custom qualifier annotation, @Reporting, meta-annotated with @Qualifier, and put it on both the bean method and the injection point.
That gives compiler checking, survives renames and makes every use of the replica easy to find.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "There are two DataSource beans, primaryDs and reportingDs. Show how to make primaryDs...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/57e883e961/