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 application.properties
- 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:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/>
</parent>
STEP 2: Add Spring Data dependency
This step remains the same.
In this example , let’s add the below:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
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:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
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 application.properties 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;
@Configuration
public class ConnectionDetails implements JdbcConnectionDetails {
@Override
public String getDriverClassName() {
return "org.h2.Driver";
}
@Override
public String getJdbcUrl() {
return "jdbc:h2:mem:testdb";
}
@Override
public String getPassword() {
return "";
}
@Override
public String getUsername() {
return "sa";
}
}
Notice that whatever configuration we did through application.properties 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;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String nickName;
public String getName() {
return name;
}
public void setName(String name) {
this.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) {
this.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;
import org.springframework.data.repository.CrudRepository;
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;
@RestController
public class TestController {
@Autowired
private UserRepository repo;
@PostMapping("/users")
public User addUser(@RequestBody User user) {
return this.repo.save(user);
}
@GetMapping("/users")
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
Reference:
https://spring.io/blog/2023/06/19/spring-boot-31-connectiondetails-abstraction
Leave a Reply