Learnitweb

Configure custom DataSource in Spring Boot

1. Introduction

If spring-boot-starter-data-jpa dependency is used, Spring will try to configure the DataSource. You just have to provide the connection details in the property file to get started. However, if you have requirement to create your own DataSource, Spring allows you to do that. In this tutorial, we’ll discuss how to configure custom DataSource in Spring Boot.

2. Configure custom DataSource in Spring Boot

Java’s javax.sql.DataSource interface provides a standard method of working with database connections. To configure custom DataSource, you have to define a bean of that type in your configuration. Spring Boot will then use your DataSource wherever it needs a DataSource.

The typical way of defining a custom DataSource is like the following:

@Configuration(proxyBeanMethods = false)
public class CustomDataSourceConfiguration {

	@Bean
	@ConfigurationProperties(prefix = "custom.datasource")
	public CustomDataSource dataSource() {
		return new CustomDataSource();
	}
} 

Note the use of prefix as custom.datasource. You can define the properties using the same prefix. Assuming CustomDataSource has JavaBean properties for username, password, url etc, these properties will be available for other components.

custom.datasource.url=jdbc:mysql://localhost:3306/mydb
custom.datasource.username=root
custom.datasource.password=admin
custom.datasource.pool-size=30

3. DataSourceBuilder

Spring also provides a convenient class DataSourceBuilder to create a standard DataSource. It can auto-detect the most suitable pooling DataSource implementation. It can also auto-detect the driver based on the JDBC URL.

Following is an example to create a DataSource by using a DataSourceBuilder:

@Configuration(proxyBeanMethods = false)
public class CustomDataSourceConfiguration {

	@Bean
	@ConfigurationProperties(prefix = "custom.datasource")
	public DataSource dataSource() {
		return DataSourceBuilder.create().build();
	}
}

You need not to do anything else other than defining @Bean of DataSource type. You can provide your connection details and get started. Wherever DataSource is required, Spring will use your defined DataSource bean. Spring will use your DataSource instead of the default one.

4. HikariDataSource with DataSourceBuilder

Following is an example of creating a HikariDataSource with DataSourceBuilder:

@Configuration(proxyBeanMethods = false)
public class CustomDataSourceConfiguration {

	@Bean
	@ConfigurationProperties(prefix = "custom.datasource")
	public HikariDataSource dataSource() {
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}
}

You can also define a custom DataSourceProperties on a custom namespace, like the following:

@Configuration(proxyBeanMethods = false)
public class CustomDataSourceConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("custom.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("custom.datasource.configuration")
    public HikariDataSource dataSource(DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }
}

This will expose the connection pool properties in the cusom.datasource.configuration sub namespace.

custom.datasource.url=jdbc:mysql://localhost:3306/mydb
custom.datasource.username=root
custom.datasource.password=admin
custom.datasource.configuration.maximum-pool-size=30

5. Example

If you want to try your custom DataSource in Spring Boot and Hibernate, you can start here. You just have to declare a @Configuration class with @Bean of type DataSource as mentioned earlier in this example (CustomDataSourceConfiguration class).