Skip to content

Commit

Permalink
Update guide to use R2DBC support in Spring Boot 2.3
Browse files Browse the repository at this point in the history
Closes gh-1.
Closes gh-2.
  • Loading branch information
mp911de committed Sep 23, 2020
1 parent 14db856 commit e050c91
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 56 deletions.
7 changes: 4 additions & 3 deletions README.adoc
@@ -1,5 +1,5 @@
:spring_version: current
:spring_boot_version: 2.2.2.RELEASE
:spring_boot_version: 2.3.4.RELEASE
:toc:
:icons: font
:source-highlighter: prettify
Expand Down Expand Up @@ -66,7 +66,8 @@ include::complete/src/main/resources/schema.sql[]

Here you have a `customer` table with three columns: `id`, `first_name`, and `last_name`.
The `id` column is auto-incremented, the other columns follow the default snake case naming scheme.
Spring Boot's auto-configuration picks up the `schema.sql` file during application startup to initialize the database schema.
Later on, we need to register a `ConnectionFactoryInitializer` to pick up the `schema.sql` file during application startup to initialize the database schema.
Because the H2 driver is on the class path and we haven't specified a connection URL, Spring Boot starts an embedded H2 database.

[[entity]]
== Define a Simple Entity
Expand Down Expand Up @@ -136,7 +137,7 @@ include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/

Now you need to modify the simple class that the Initializr created for you.
To get output (to the console, in this example), you need to set up a logger.
Then you need to set up some data and use it to generate output.
Then you need to set up the initializer to setup the schema and some data and use it to generate output.
The following listing shows the finished `AccessingDataR2dbcApplication` class (in `src/main/java/com/example/accessingdatar2dbc/AccessingDataR2dbcApplication.java`):

====
Expand Down
2 changes: 1 addition & 1 deletion complete/build.gradle
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
Expand Down
21 changes: 2 additions & 19 deletions complete/pom.xml
Expand Up @@ -10,12 +10,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<version>2.3.4.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
Expand All @@ -34,30 +34,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<java.version>1.8</java.version>
</properties>
Expand Down
@@ -1,11 +1,15 @@
package com.example.accessingdatar2dbc;

import io.r2dbc.spi.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.r2dbc.connectionfactory.init.ConnectionFactoryInitializer;
import org.springframework.data.r2dbc.connectionfactory.init.ResourceDatabasePopulator;

import java.time.Duration;
import java.util.Arrays;
Expand All @@ -19,6 +23,16 @@ public static void main(String[] args) {
SpringApplication.run(AccessingDataR2dbcApplication.class, args);
}

@Bean
ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));

return initializer;
}

@Bean
public CommandLineRunner demo(CustomerRepository repository) {

Expand Down
3 changes: 0 additions & 3 deletions complete/src/main/resources/application.yml
@@ -1,3 +0,0 @@
spring:
r2dbc:
url: "r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"
14 changes: 3 additions & 11 deletions initial/build.gradle
@@ -1,6 +1,6 @@
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}

Expand All @@ -10,26 +10,18 @@ sourceCompatibility = '1.8'

repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
implementation 'org.springframework.boot.experimental:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'io.r2dbc:r2dbc-h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc'
testImplementation 'io.projectreactor:reactor-test'
}

dependencyManagement {
imports {
mavenBom 'org.springframework.boot.experimental:spring-boot-bom-r2dbc:0.1.0.M3'
}
}

test {
useJUnitPlatform()
}
21 changes: 2 additions & 19 deletions initial/pom.xml
Expand Up @@ -10,12 +10,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<version>2.3.4.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
Expand All @@ -34,30 +34,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<java.version>1.8</java.version>
</properties>
Expand Down

0 comments on commit e050c91

Please sign in to comment.