Spring 数据 r2dbc:与 mysql 连接时出现问题 - 无法找到 'r2dbcEntityTemplate'

Spring data r2dbc: Problem connecting with mysql - 'r2dbcEntityTemplate' could not be found

我的 spring 引导应用程序正在尝试使用 spring 数据 r2dbc 连接到 mysql 5.7。设置看起来非常简单。然而,我一直遇到意外的异常。

如有任何帮助,我们将不胜感激。

日志:

2021-09-28 10:13:48.591  INFO 12261 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2021-09-28 10:13:48.651  INFO 12261 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 52 ms. Found 1 R2DBC repository interfaces.
//...
2021-09-28 10:13:49.250  WARN 12261 --- [  restartedMain] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 
//...
Error creating bean with name 'healthCheckRepository' defined in application.model.respository.r2dbc.HealthCheckRepository defined in @EnableR2dbcRepositories declared on MainApplication: Cannot resolve reference to bean 'r2dbcEntityTemplate' while setting bean property 'entityOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'r2dbcEntityTemplate' available
//... 
Description:

Parameter 0 of constructor in application.service.HealthCheckService required a bean named 'r2dbcEntityTemplate' that could not be found. 


Action:

Consider defining a bean named 'r2dbcEntityTemplate' in your configuration.

application.yaml

spring:
  r2dbc:
    url: r2dbc:pool:mysql//server/database?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLocalSessionState=true
    username: username
    password: password
    pool:
      enabled: true
      max-size: 20
      validation-query: SELECT 1
      initial-size: 5
      max-idle-time: 30m
  data:
    r2dbc:
      repositories:
        enabled: true

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id "io.freefair.lombok" version "6.2.0" //using
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'

//    implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'

    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    runtimeOnly 'dev.miku:r2dbc-mysql:0.8.2.RELEASE'
    runtimeOnly 'mysql:mysql-connector-java'
}

原来是 Spring 引导问题。请参考以下git问题解决

https://github.com/spring-projects/spring-data-r2dbc/issues/659