spring数据redis invocationtargetexception

xfyts7mz  于 2021-06-10  发布在  Redis
关注(0)|答案(1)|浏览(537)

我目前正在尝试SpringDataRedis,以便获取数据并将其放入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依赖。

7rtdyuoh

7rtdyuoh1#

我被一个类似的问题困扰了将近两天,但最终还是解决了。问题是,当在一个项目中使用多个spring数据模块时,spring框架进入严格的存储库配置模式,并在下面使用不同的检测机制,以确定哪个存储库属于哪个持久性技术(这里解释)。
在您的例子中,使用了两个spring数据模块,即spring数据jdbc和spring数据redis。因此,项目中定义的profilerepository和metadatarepository可以用作spring数据jdbc和spring数据redis的存储库。解决这个问题的一种方法是使用特定于模块的注解。例如,要将profilerepository定义为与redis相关的存储库,必须将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
}

相关问题