簡體   English   中英

Spring 數據 Redis InvocationTargetException

[英]Spring Data Redis InvocationTargetException

我目前正在嘗試處理 Spring 數據 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]

我的代碼中有兩個存儲庫接口, MetadataRepositoryProfileRepository ,由於某種原因,錯誤在這兩者之間不斷來回,具有相同的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-jdbcspring-boot-starter-data-redis在我的構建中引起了沖突,導致了InvocationTargetException 排除 JDBC 依賴項后, InvocationTargetException消失。 但是,這樣做會破壞需要 JDBC 連接的其他模塊。 因此,在我的案例中,我需要 JDBC 和 Redis 依賴項。

我被類似的問題困擾了將近兩天,但終於弄明白了。 問題是,當一個項目中使用多個 Spring 數據模塊時,Spring 框架會進入嚴格的存儲庫配置模式,並在底層使用不同的檢測機制,以識別哪個存儲庫屬於哪種持久化技術( 此處解釋)。

在您的情況下,使用了兩個 Spring 數據模塊,即 spring-data-jdbc 和 spring-data-redis。 因此,在您的項目中定義的 ProfileRepository 和 MetadataRepository 可以用作 spring-data-jdbc 和 spring-data-redis 的存儲庫。 解決此問題的一種方法是使用模塊特定注釋 例如,要將 ProfileRepository 定義為與 Redis 相關的 Repository,則必須將您的 Profile class 定義為:

@RedisHash("Profile")
public class Profile implements Serializable {
// fields and getters/setters as usual
}

或者,如果您想將 ProfileRepository 定義為與 JDBC 相關的 Repository,則必須將您的 Profile class 定義為:

@Table("Profile")
public class Profile implements Serializable {
// fields and getters/setters as usual
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM