java - Spring Data Redis InvocationTargetException
问题描述
我目前正在尝试使用 Spring Data Redis,以便获取数据并将其放入 redis 存储库。但是,在使用 运行我的应用程序时mvn spring-boot:run
,它总是返回错误并显示以下消息:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.7.RELEASE:run (default-cli) on project networkprofile-bs-redis-poc: An exception occurred while running. null: InvocationTargetException: Invalid bean definition with name 'metadataRepository' defined in null: Cannot register bean definition [Root bean: class [org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'metadataRepository': There is already [Root bean: class [org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound. -> [Help 1]
我的代码中有两个存储库接口,MetadataRepository
并且ProfileRepository
由于某种原因,错误在这两者之间不断来回切换,并且具有相同的InvocationTargetException
.
元数据存储库
package xxx.repository;
import org.springframework.data.repository.CrudRepository;
import xxx.model.redis.Metadata;
public interface MetadataRepository extends CrudRepository<Metadata, String> {}
配置文件库
package xxx.repository;
import org.springframework.data.repository.CrudRepository;
import xxx.model.redis.Profile;
public interface ProfileRepository extends CrudRepository<Profile, String> {
Profile findByMsisdnAndProfile(String msisdn, String profile);
}
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
...
<axiom.version>1.2.21</axiom.version>
<springfox.version>2.9.2</springfox.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>${axiom.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>${axiom.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
我设置了一些 Redis 配置如下:
package xxx.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Value("${xxx.redis.host}")
private String redisHost;
@Value("${xxx.redis.port}")
private int redisPort;
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory());
template.afterPropertiesSet();
return template;
}
}
我不太确定是什么原因造成的,因为这与抛出的类似问题不同BeanDefinitionOverrideException
。
我的目标是让我的应用程序可以毫无问题地运行,所以我需要查明造成这种情况的原因。也许我做错了什么?
更新
我发现spring-boot-starter-data-jdbc
并spring-boot-starter-data-redis
导致我的构建中发生冲突,导致InvocationTargetException
. 排除 JDBC 依赖项后,InvocationTargetException
消失。但是,这样做会破坏我需要 JDBC 连接的其他模块。所以在我的情况下,我需要 JDBC 和 Redis 依赖项。
解决方案
我被类似的问题困扰了将近两天,但终于弄清楚了。问题是,当一个项目中使用多个 Spring Data 模块时,Spring 框架进入严格的存储库配置模式,并在下面使用不同的检测机制,来识别哪个存储库属于哪种持久化技术(这里解释)。
在您的情况下,使用了两个 Spring Data 模块,即 spring-data-jdbc 和 spring-data-redis。因此,项目中定义的 ProfileRepository 和 MetadataRepository 可以用作 spring-data-jdbc 和 spring-data-redis 的存储库。解决此问题的一种方法是使用特定于模块的注释。例如,要将 ProfileRepository 定义为与 Redis 相关的 Repository,您必须将 Profile 类定义为:
@RedisHash("Profile")
public class Profile implements Serializable {
// fields and getters/setters as usual
}
或者,如果要将 ProfileRepository 定义为与 JDBC 相关的存储库,则必须将 Profile 类定义为:
@Table("Profile")
public class Profile implements Serializable {
// fields and getters/setters as usual
}
推荐阅读
- elasticsearch - 使用相同脚本对多个文档进行 Elasticsearch 批量更新
- continuous-integration - 当 composer.lock 使用 SamKirkland/FTP-Deploy-Action@3.1.1 通过 github 操作更改时,如何上传供应商/文件夹
- android - 如何在 Android 上发出 GET 请求?
- mysql - 如何获取mysql中每个用户的最新消息?
- javascript - 在 laravel7 中使用 ajax 和 jquery 导致下拉格式不显示在 html 中
- database - Postgresql 函数有不支持的返回
- reactjs - 'Spring' 不是从'react-spring' 导出的
- javascript - 为什么我必须在表单中的“提交”按钮上按两次才能看到反应中的日志?
- spring - 使用 Java 8 并行流 API 的 SMTP 身份验证异常
- flutter - 来自多个 Firebase 项目的 Flutter FCM 主题消息