Spring Boot Configures Multiple DataSource

When using Spring Boot, it is very easy to configure DataSource by default. Spring Boot will automatically configure a DataSource for us.

If you specify the configuration of spring.datasource in application.yml, Spring Boot will use this configuration to create a DataSource. If the application.yml does not specify any spring.datasource related configuration, Spring Boot will search in the classpath H2, hsqldb memory database jar package, if found, it will automatically configure a memory database DataSource, so we As long as the introduction of jar package can be. For example, configure a hsqldb data source:

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

However, in some cases, if we need to configure multiple data sources, how should the Spring Boot configuration?

We use JDBC as an example to demonstrate how to configure two DataSource in Spring Boot. Corresponding, we will create two JdbcTemplate the Bean, respectively, the use of these two data sources.

Spring Boot Configures Multiple DataSource

First, we have to declare the configuration of two data sources in application.yml, one using spring.datasource and the other using spring.second-datasource:

spring:
  application:
    name: data-multidatasource
  datasource:
    driver-class-name: org.hsqldb.jdbc.JDBCDriver
    url: jdbc:hsqldb:mem:db1
    username: sa
    password:
  second-datasource:
    driver-class-name: org.hsqldb.jdbc.JDBCDriver
    url: jdbc:hsqldb:mem:db2
    username: sa
    password:

Both DataSource use hsqldb, but the database is different. In addition, when using multiple data sources, all necessary configurations can not be omitted.

Second, we need to create two DataSource beans, one marked @Primary and the other named secondDatasource:

@Configuration
public class SomeConfiguration {
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondDatasource")
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

For each DataSource, we must specify the prefix of the configuration item with @ConfigurationProperties (prefix = “xxx”).

Then, we create two JdbcTemplate Bean, one marked @Primary, the other named secondJdbcTemplate, respectively, using the corresponding DataSource:

@Bean
@Primary
public JdbcTemplate primaryJdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

@Bean(name = "secondJdbcTemplate")
public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDatasource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

Note that when the secondJdbcTemplate is created, the incoming DataSource must be declared with @Qualifier (“secondDatasource”) so that the second DataSource can be used.

Now we have created two JdbcTemplate beans. Where you need to use the first JdbcTemplate, we inject it directly:

@Component
public class SomeService {
    @Autowired
    JdbcTemplate jdbcTemplate;
}

Where you need to use the second JdbcTemplate, we need to use @Qualifier (“secondJdbcTemplate”) to sign in:

@Component
public class AnotherService {
    @Autowired
    @Qualifier("secondJdbcTemplate")
    JdbcTemplate secondJdbcTemplate;
}

In this way, we can for different data sources, with different JdbcTemplate operation.

Precautions

When there are multiple instances of beans of the same type, for example, multiple DataSource, multiple JdbcTemplates, it is strongly recommended that you always use @Primary to identify one of the beans as “primary” and use @Autowired to inject the first @Primary’s Bean.

The same type of other beans, each need to use @Bean (name = “xxx”) to identify the name, and, when using @Autowired injection with @Qualifier (“xxx”) specify the name of the injected bean.

Complete example source code Please refer to:

https://github.com/michaelliao/springcloud/tree/master/data-multidatasource