How to connect to database using ConnectionDetails interface in Spring Boot?

Let’s say you want to connect to a database in your Spring Boot application.

You first add Spring Data dependency.

Then to configure the database details , you had two options:

  • Using
  • Using DataSource bean configuration in Java file

Starting Spring Boot 3.1.1 you have one more option.

Using ConnectionDetails interface.

Let’s see how to do that with an example.

STEP 1: Add Spring Boot Starter Parent 3.1.1 or later version

Here is the dependency I got while generating the project through Spring Initializr:


STEP 2: Add Spring Data dependency

This step remains the same.

In this example , let’s add the below:


STEP 3: Add Database driver dependency

Next you need to add the database driver dependency.

This step also remains the same.

In this example , let’s use in memory database .

Let us add the below dependency for H2 in memory database:


STEP 4: Extend ConnectionDetails interface

This is the only step which is different from the other two options.

Instead of configuring database url , user name and password etc in or using DataSource bean , you need to configure them in a class which implements ConnectionDetails interface provided since Spring Boot 3.1.1.

To more specific,

ConnectionDetails is a parent interface and you have child interfaces like JdbcConnectionDetails , MongoConnectionDetails etc.

In our case let’s use JdbcConnectionDetails as we are going to connect to a relational database.

Here is the configuration code:

package com.connection.details.connectiondemo;

import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.context.annotation.Configuration;

public class ConnectionDetails implements JdbcConnectionDetails {

    public String getDriverClassName() {
        return "org.h2.Driver";

    public String getJdbcUrl() {
        return "jdbc:h2:mem:testdb";


    public String getPassword() {
        return "";

    public String getUsername() {
        return "sa";


Notice that whatever configuration we did through we have done through the above class which implements JdbcConnectionDetails interface.

STEP 5: Create an entity class

This step also remains the same.

Here is a sample entity class :

package com.connection.details.connectiondemo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Table(name = "users")
public class User {

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private long id;

    private String name;

    private String nickName;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getNickName() {
        return nickName;

    public void setNickName(String nickName) {
        this.nickName = nickName;

    public long getId() {
        return id;

    public void setId(long id) { = id;


STEP 6: Create a repository interface

This step also remains the same.

You create a repository by extending CRUDRepository or JpaRepository.

Here is a sample:

package com.connection.details.connectiondemo;


public interface UserRepository extends CrudRepository<User, Long> {


That’s it.

Now you can test your changes.

STEP 7 : Test

To test let’s create a sample controller which exposes an API to add an user to the database and another API to fetch all the users .

Before that make sure you have added spring web dependency.

Here is the sample controller class:

package com.connection.details.connectiondemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class TestController {

    private UserRepository repo;

    public User addUser(@RequestBody User user) {


    public Iterable<User> getUser() {

        return this.repo.findAll();

I hit the API with sample data:

And then retrieved the entry from db:

It works!

Spring introduced this feature mainly to support automatic execution of Docker Compose.

We will look into that in another post!

Here is the entire code:

Connection Details Source Code



Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading