带有JPA和R2DBC的Spring Boot 2.4混合项目无法启动



我正在处理一个现有的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>

我有以下作为我的配置类:

@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$0(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$$5198d258.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

我不明白为什么会发生这种情况——我们确实使用传统的SimpleJpaRepository实现来实现我们的DAO/它们都没有扩展ReactiveCrudRepository,因此不应该是";"看见";从应用程序的角度来看,作为被动存储。

无论如何,我想知道其他人是否也面临过类似的情况和可能的解决方案。

检查我对这个问题的回答,我还提供了一个工作示例。

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

最新更新