SpringBoot2.4与jpa和r2dbc的混合项目无法启动

lb3vh1jj  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(476)

我正在从事一个现有的springboot2.4项目,并试图将webflux和r2dbc引入一个现有的基于jpa的应用程序中。
我可以引入webflux依赖项:

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

没有任何问题,我的应用程序启动和行为,因为它应该,能够执行一些测试使用通量/单声道和一切都很好。
然而,为了获得完全的React性,我需要一直到持久层,也就是说,引入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

我不明白为什么会发生这种情况-我们的dao使用传统的simplejparepository实现/没有一个扩展了reactivecrudepository,因此从应用程序的Angular 来看不应该被“视为”React存储。
在任何情况下,我想知道是否其他人也遇到过类似的情况和可能的解决方法/修复方法。

at0kjp5o

at0kjp5o1#

检查我对这个问题的回答,我还提供了一个工作示例。
顺便说一句,我不认为在示例应用程序中混合阻塞jpaapi和不阻塞r2dbcapi是一个好的选择。

相关问题