使用反应式数据库客户端扩展 Spring WebFlux 应用程序时出现问题:冲突的 Maven 依赖项?

Problem extending Spring WebFlux application with reactive DB client: conflicting Maven dependencies?

我有一个基于此代码制作的小型 Spring WebFlux 应用程序:

https://github.com/chang-chao/spring-webflux-reactive-jdbc-sample

据我所知,这是纯反应式编程和通常的阻塞关系数据库之间的某种混合。

现在我的任务是将反应式数据库客户端添加到我的应用程序。我盯着这个指南:

https://spring.io/guides/gs/accessing-data-r2dbc/

但是一旦我将以下依赖项添加到我的 pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-h2</artifactId>
    <version>0.8.4.RELEASE</version>
    <scope>runtime</scope>
</dependency>

我的 WORKING 应用程序启动失败,提示找不到自动装配的存储库 bean。一旦我删除了上面的两个依赖项,这个错误就消失了。

工作应用的 pom.xml 初始完整:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.freelance</groupId>
    <artifactId>studentlist</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>student-list</name>
    <description>Spring WebFlux application for managing students</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.4.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <version>3.3.10.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我想有些依赖项相互冲突。有没有办法将这些依赖项与我的所有其他依赖项一起实际使用,以便我能够遵循该指南?

不知道这个问题是否需要app的Java代码,如果需要,需要哪几段。现在我只添加 application.properties:

spring.h2.console.enabled=true
spring.h2.console.path=/h2_console

spring.datasource.url=jdbc:h2:~/studentlist
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true

logging.level.org.springframework=warn
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
logging.level.org.hibernate.SQL=warn
logging.level.io.netty=warn

spring.datasource.maximum-pool-size=100

添加其中之一这两个依赖项,spring-boot-starter-data-r2dbcr2dbc-h2,没有第二个就足以导致这个错误:

2020-10-20 15:31:26.008  WARN 11580 --- [           main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.freelance.studentlist.repository.StudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-10-20 15:31:26.123 ERROR 11580 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studentRepository in com.freelance.studentlist.service.StudentServiceImpl required a bean of type 'com.freelance.studentlist.repository.StudentRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.freelance.studentlist.repository.StudentRepository' in your configuration.


Process finished with exit code 1

我删除了 JPA 依赖项,目前我正在尝试只使用 r2dbc。

我想我的 application.properties 将不再有效。你能帮我改一下吗?我使用指南中的代码片段:

public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
    @Bean
    public H2ConnectionFactory connectionFactory() {
        return new H2ConnectionFactory(
                H2ConnectionConfiguration.builder()
                        .url("jdbc:h2:~/studentlist;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4")
                        .username("sa")
                        .build());
    }
}

我强烈怀疑这样的url对r2dbc无效。在 r2dbc H2(不是内存中,只是本地数据库)的情况下,jdbc:h2:~/studentlist URL 的有效替代是什么?

我应该如何更改 application.properties 中的这段代码? URL 特别是!

**spring.datasource.url=jdbc:h2:~/studentlist**
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

知道的请帮忙!现在不能google一个合适的例子...

spring.datasource 用于传统的 Jdbc 数据源,要配置 R2dbc 连接,请在 Spring 引导 application.properties 中使用 spring.r2dbc 前缀。

检查 my Spring R2dbc example,如果您是 Spring Data R2dbc 的新手,Readme.md.

中的文档