How to setup datasources dynamically in springboot
1. Introduction
This post would demo how to setup the datasources dynamically in the spring or springboot application.
2. Environments
SpringBoot 1.5.12+
Java 1.8
3. The Pom.xml
spring boot version:
all dependencies:
4. Three ways to setup datasources
To summarize, there are three ways to setup the datasource in spring application:
The default way
setup via the application.properties, in springboot app, the process is automatically, you just configure the database properties and then you can use the JdbcTemplate object
The dynamic ways:
2.1 Setup via DataSourceBuilder and application.properties in a @Configuration Bean
2.2 Setup only by DataSourceBuilder, there is no properties file needed
Just as the picture shows:
5. The database and table
For demo purpose, I setup one database in localhost as follows:
There is a table ‘tbl_student’ in both of the databases:
And insert one record into that table:
6. The domain class Student
7. The default way: setup via the application.properties
7.1 the application.properties
Supply these properties in your application.properties:
7.2 the DAO class
The DAO class:
7.3 The JUnit testcase
Run the testcase, we got a green bar.
8. The dynamic way 1:Setup via DataSourceBuilder and application.properties
8.1 the application.properties
Supply these properties in your application.properties:
8.2 The config bean for the datasource
Here we did these:
define a dynamic datasource named ‘dsMaster’ and use the DatasourceBuilder to create it
define a jdbcTemplate named ‘jdbcMaster’ to point to the datasource ‘dsMaster’
8.3 the DAO class
The DAO class:
We only add one line:
@Qualifier(“jdbcMaster”)
to the jdbcTemplateObject.
8.4 The JUnit testcase
Run the testcase, we got a green bar.
9. The dynamic way 2:Setup only by DataSourceBuilder
There is no need for application.properties, we would define the database total dynamically.
9.1 The config bean for the datasource
Here we did these:
define a dynamic datasource named ‘dsCustom’ and use the DatasourceBuilder to create it, the datasource properties is supplied dynamically in java code
define a jdbcTemplate named ‘jdbcCustom’ to point to the datasource ‘dsCustom’
9.2 the DAO class
The DAO class:
We only change one line:
@Qualifier(“jdbcCustom”)
Then everything is done.
9.3 The JUnit testcase
Run the testcase, we got a green bar.
10 The DataSourceBuilder
This post used the DataSourceBuilder to create the datasource, The class description is as follows:
Convenience class for building a DataSource with common implementations and properties. If HikariCP, Tomcat or Commons DBCP are on the classpath one of them will be selected (in that order with Hikari first). In the interest of a uniform interface, and so that there can be a fallback to an embedded database if one can be detected on the classpath, only a small set of common configuration properties are supported. To inject additional properties into the result you can downcast it, or use @ConfigurationProperties.
here is a list of the methods of it:
It’s so easy, do you think so?
The example code has been uploaded to github, you can visit here to view the example codes.
You can find detail documents about the springboot and dynamic datasource here: