在R2DBC 入门视频之后,我将一些存储库转换为以 PostgreSQL 作为数据库的现有 Spring Boot 应用程序中的反应式。该应用程序在转换之前工作。在我尝试启动应用程序后,我收到以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albumReactResource' defined in file [/app/classes/com/mycompany/myapp/web/rest/react/AlbumReactResource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albumReactRepository' defined in class path resource [com/mycompany/myapp/config/ReactDatabaseConfig.class]: Unsatisfied dependency expressed through method 'albumReactRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r2dbcRepositoryFactory' defined in class path resource [com/mycompany/myapp/config/ReactDatabaseConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader@4e0e2f2a]
...
gallery-app_1 | Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.query.QueryMethodEvaluationContextProvider
gallery-app_1 | at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
gallery-app_1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
gallery-app_1 | at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
gallery-app_1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
gallery-app_1 | ... 51 common frames omitted
QueryMethodEvaluationContextProvider 类应该在那里。我猜数据库配置不能与现有数据库和 Spring Data 配置共存。
Spring Boot 版本是 2.0.5.RELEASE。相关依赖如下:
compile "org.springframework.boot:spring-boot-starter-webflux"
compile "org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT"
compile "io.r2dbc:r2dbc-postgresql:1.0.0.M5"
这是与视频代码相同的数据库配置:
配置公共类 ReactDatabaseConfig {
@Bean
PostgresqlConnectionFactory connectionFactory(){
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host("localhost")
.database("gallery")
.username("postgres")
.password("")
.build()
);
}
@Bean
DatabaseClient databaseClient(ConnectionFactory connectionFactory){
return DatabaseClient.builder()
.connectionFactory(connectionFactory)
.build();
}
@Bean
R2dbcRepositoryFactory r2dbcRepositoryFactory(DatabaseClient client){
RelationalMappingContext context = new RelationalMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context);
}
@Bean
AlbumReactRepository albumReactRepository(R2dbcRepositoryFactory factory){
return factory.getRepository(AlbumReactRepository.class); <-- exceptions occurs
}
...
}
一些相关的依赖信息如下:
+--- org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT
| +--- org.springframework.data:spring-data-commons:2.1.1.BUILD-SNAPSHOT -> 2.0.10.RELEASE (*)
...
+--- org.springframework.security:spring-security-data -> 5.0.8.RELEASE
| +--- javax.xml.bind:jaxb-api:2.3.0
| +--- org.springframework.data:spring-data-commons:2.0.10.RELEASE (*)
...
| +--- org.springframework.data:spring-data-jpa:2.0.10.RELEASE
| | +--- org.springframework.data:spring-data-commons:2.0.10.RELEASE
如何解决这个问题?