Spring Boot DataSourceBuilder tutorial

last modified July 6, 2020

Spring Boot DataSourceBuilder tutorial shows how to use DataSourceBuilder to create datasources in a command line Spring Boot application. A HikariCP connection pool is used.

DataSourceBuilder is a Java convenience class to create a data source with common implementations and properties.

H2 is an open source relational database management system created entirely in Java. It can be embedded in Java applications or run in the client-server mode. It is easy to deploy and install and has small footprint.

Spring is a Java application framework for developing Java enterprise applications. It also helps integrate various enterprise components. Spring Boot makes it easy to create Spring-powered, production-grade applications and services with minimum setup requirements.

Spring Boot DataSourceBuilder example

The following is a simple Spring Boot console application. It retrieves data from the H2 in-memory database and displays it in the terminal. To configure the datasource, we use the DataSourceBuilder class.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           │   MyRunner.java
│   │           ├───config
│   │           │       AppConfig.java
│   │           └───model
│   │                   Car.java
│   └───resources
│           application.yaml
│           data-h2.sql
│           logback.xml
│           schema-h2.sql

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"









The Maven pom.xml we declare the necessary dependencies.

package com.zetcode.model;

import java.util.Objects;

public class Car {

    private Long id;
    private String name;
    private int price;

    public Car() {}

    public Car(Long id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return price == car.price &&
                Objects.equals(id, car.id) &&
                Objects.equals(name, car.name);

    public int hashCode() {
        return Objects.hash(id, name, price);

    public String toString() {
        final StringBuilder sb = new StringBuilder("Car{");
        sb.append(", name='").append(name).append('\'');
        sb.append(", price=").append(price);
        return sb.toString();

This is Car bean class. It contains item id, name, and price.

    name VARCHAR(255), price INT);

This SQL script creates the cars table.

INSERT INTO cars(name, price) VALUES('Audi', 52642);
INSERT INTO cars(name, price) VALUES('Mercedes', 57127);
INSERT INTO cars(name, price) VALUES('Skoda', 9000);
INSERT INTO cars(name, price) VALUES('Volvo', 29000);
INSERT INTO cars(name, price) VALUES('Bentley', 350000);
INSERT INTO cars(name, price) VALUES('Citroen', 21000);
INSERT INTO cars(name, price) VALUES('Hummer', 41400);
INSERT INTO cars(name, price) VALUES('Volkswagen', 21600);

This script fills the table with data. Both scripts are located in the root of the classpath.

<?xml version="1.0" encoding="UTF-8"?>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
    <logger name="com.zetcode" level="DEBUG"/>
    <logger name="com.zaxxer.hikari" level="INFO"/>

In the logback.xml file, we configre the logging levels. We set the logging level for Spring framework to ERROR so that our output is not cluttered with unnecessary details.

    minimum-idle: 1
    maximum-pool-size: 20
  jdbcUrl: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
  driverClassName: org.h2.Driver

    banner-mode: "off"

    platform: h2

The main Spring Boot configuration file is called application.yml. In datasource property we configure the datasource and HikariCP. We use an in-memory H2 database.

With the banner-mode property we turn off Spring Boot banner. The platform value is used in SQL initialization scripts: schema-${platform}.sql and data-${platform}.sql.

package com.zetcode.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

public class AppConfig {

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

A datasource is generated in AppConfig. With the @ConfigurationProperties annotation, we have externalized the configuration into the YAML file.

package com.zetcode;

import com.zetcode.model.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

public class MyRunner implements CommandLineRunner {

    private JdbcTemplate jdbcTemplate;

    public void run(String... args) {

        var sql = "SELECT * FROM cars";

        var cars = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Car.class));

        for (Car car: cars) {


MyRunner executes an SQL query and shows the output in the console.

private JdbcTemplate jdbcTemplate;

JdbcTemplate is injected.

var sql = "SELECT * FROM cars";

This is SQL to be executed. We select all cars from the cars table.

var cars = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Car.class));

BeanPropertyRowMapper converts a row into a new instance of the specified mapped target class.

for (Car car: cars) {

We iterate over all car objects and print them to the console.

package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

The Application sets up the Spring Boot application.

$ mvn -q spring-boot:run
Car{id=1, name='Audi', price=52642}
Car{id=2, name='Mercedes', price=57127}
Car{id=3, name='Skoda', price=9000}
Car{id=4, name='Volvo', price=29000}
Car{id=5, name='Bentley', price=350000}
Car{id=6, name='Citroen', price=21000}
Car{id=7, name='Hummer', price=41400}
Car{id=8, name='Volkswagen', price=21600}

We run the Spring Boot application. The eight cars are displayed.

In this tutorial, we used DataSourceBuilder in a Spring Boot console application.

List all Spring Boot tutorials.