0

我有一个基于此代码的小型 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。一旦我删除了上面的两个依赖项,这个错误就消失了。

WORKING 应用程序的初始完整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>

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

我不知道这个问题是否需要应用程序的 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-r2dbc不添加r2dbc-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 无效。jdbc:h2:~/studentlist如果是 r2dbc H2(不是内存中,只是本地数据库),什么是 URL 的有效替换?

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

**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

知道的请帮忙!现在无法谷歌一个合适的例子......

4

1 回答 1

0

对于spring.datasource传统的 Jdbc 数据源,要配置 R2dbc 连接,请在 Spring Boot application.propertiesspring.r2dbc中使用前缀代替。

查看我的 Spring R2dbc 示例,如果您不熟悉 Spring Data R2dbc,请查看 Readme.md 中的文档。

于 2020-11-22T10:34:17.457 回答