Spring Boot - Multiple Data-Sources
This article is about configuring multiple data-sources in Spring Boot applications.
The convention over configuration in Spring Boot is to configure a single data-source. This is done via the
spring.datasource.* properties and the configuraton classes from the Spring Boot package
When having a look at class
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration you will see that it defines multiple
@Configuration classes for the various environments where data-sources can be defined (e.g. with Tomcat, in a DBCP2 connection pool etc.). Each of the
@Bean methods configure a data-source. The data-source properties are set with class
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties which is basically a
@ConfigurationProperties container for the property values:
As you can see above, all the properties found with prefix
spring.datasource are bound against an instance of this class.
Now let’s say we want to introduce a second data-source besides the Spring Boot configured one. In order to do so, we have to overwrite the default
DataSource bean and mark it as primary one to be used by Spring and we have to define a second
When configuring the beans, we can reuse Spring Boot’s
DataSourceProperties in that we use it in a
@Bean method we annotate with
@ConfigurationProperties. In such a way we can have exactly the same property names for configuring the data-source bean as Spring Boot, but mapped to another prefix:
DataSourceConfiguration from above defines a new primary
java.sql.DataSource bean. The
@Primary annotation is important so that this data-source is treated as primary data-source that has precedence for Spring Boot’s auto-configuration mechanism e.g. when creating an
Let’s add the second data-source:
We do so by adding a nested
@Configuration being only active if the property value
app.opt.datasource.active is set to
true. That’s basically what
@ConditionalOnProperty(name = "app.optdatasource.active") does. It checks the
app.opt.datasource.active property and if it is present and
@Configuration will get executed and the second data-source bean will be defined.
If course, you could even add more data-sources, for example, you could add data-sources depending on the current Spring environment/profile currently active. But the general scheme would be the same.
This article showed how to configure multiple data-sources in a Spring Boot application. Besides the default data-source, we add an optional data-source that can be used from within the application code for special purposes.