Spring 使用 JPA 和 R2DBC 的 Boot 2.4 混合项目无法启动

Spring Boot 2.4 mixed project with JPA and R2DBC doesn't start

我正在处理现有的 Spring Boot 2.4 项目,并尝试将 Webflux 和 R2DBC 引入现有的基于 JPA 的应用程序。

我可以引入 Webflux 依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

没有任何问题,我的应用程序启动并正常运行,能够使用 Flux/Mono 执行一些测试,一切正常。

然而,为了获得完全的反应性,我需要一直深入到持久层,即也引入 R2DBC 依赖项(对于 postgres)

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

我的配置如下class:

@Configuration
@EnableR2dbcRepositories(
        repositoryBaseClass = ReactiveCrudRepository.class
)
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {

    @Bean
    @Qualifier("rxConnectionFactory")
    @Override
    public ConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration
                .builder()
                .username("user")
                .password("password")
                .host("localhost")
                .port(5432)
                .database("my_db")
                .build()
        );
    }
}

有了这个依赖,我的项目无法启动,始终给出相同的异常:

Caused by: java.lang.IllegalStateException: Cannot apply reactive transaction to non-reactive return type: interface java.util.List
    at org.springframework.transaction.interceptor.TransactionAspectSupport.lambda$invokeWithinTransaction[=14=](TransactionAspectSupport.java:351)
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:346)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:134)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
    at com.mycompany.myapplication.service.SettingsServiceImpl$$EnhancerBySpringCGLIB$98d258.findAll(<generated>)
    at com.mycompany.myapplication.service.RuntimeSettingsServiceImpl.populateMap(RuntimeSettingsServiceImpl.java:96)
    at com.mycompany.myapplication.service.RuntimeSettingsServiceImpl.init(RuntimeSettingsServiceImpl.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
    ... 109 common frames omitted

我无法理解为什么会发生这种情况 - 我们确实为我们的 DAO 使用了传统的 SimpleJpaRepository 实现/none 其中扩展了 ReactiveCrudRepository,因此不应被“视为”来自应用程序的反应式商店观点。

无论如何,我想知道其他人是否遇到过类似的情况和可能的 workaround/fix。

检查我对 , and I also provided a working example 的回答。

顺便说一句,我认为在示例应用程序中混合阻塞 JPA API 和 none-阻塞 R2dbc API 不是一个好的选择。