NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute

2022-09-04 07:21:10

我正在尝试在spring-boot应用程序中使用spring-data-redis来使用redis。我正在创建如下:JedisConnectionFactory

RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);

它引发异常:

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)Ljava/util/Optional;
    at org.springframework.data.redis.repository.configuration.RedisRepositoryConfigurationExtension.registerBeansForRoot(RedisRepositoryConfigurationExtension.java:88)
    at org.springframework.data.repository.config.RepositoryConfigurationDelegate.registerRepositoriesIn(RepositoryConfigurationDelegate.java:118)
    at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:59)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:352)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:143)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:336)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:246)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
    at com.test.redis.RedisTesterApplication.main(RedisTesterApplication.java:11)

My build.gradle:

dependencies {
    compile('org.springframework.data:spring-data-redis:2.0.2.RELEASE')
    compile('redis.clients:jedis:2.9.0')
    compile('org.json:json:20160810')
    compile('org.springframework.boot:spring-boot-starter:1.4.2.RELEASE')
    compile("org.springframework:spring-web")
    compile('org.slf4j:slf4j-api:+')
}

是因为 spring-boot 和 spring-data-redis 的依赖项版本不兼容吗?如何知道要使用哪些版本?


答案 1

您的问题来自Spring Boot,Spring Data Commons和Spring Data Redis之间的不兼容性。

理想情况下,在使用Spring Boot的Gradle插件时,您无需指定任何版本,因为Spring Boot为您的依赖项提供了依赖项管理,可确保引用库之间的兼容性。

通常,Spring Boot应该是依赖项版本的主版本。Spring Data 2.x 与 Spring Boot 1.x 不兼容。请升级到最近的Spring Boot里程碑(截至目前为2.0 M7)或将Spring Data Redis降级到1.7.x。


答案 2

尽管许多人从此框架中受益,但有时需要更多时间对其依赖项进行故障排除。这是一个已知问题,我遇到了与下面相同的问题。

java.lang.NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)Ljava/util/Optional; at org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension.postProcess(JpaRepositoryConfigExtension.java:125) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]

我注意了第一个jar文件及其版本 - 2.0.2并删除了该文件。spring-data-jpa-2.0.2.RELEASE.jar.之后,我让家长决定它的依赖关系,并下载了1.10.5版本。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
 ....
</dependencies>

spring-data-jpa-2.0.2.RELEASE.jar (与父级冲突)

spring-data-jpa-1.10.5.RELEASE.jar (解析版本)

每个人在遇到此错误时都可能有不同的版本。但是,如果您先仔细检查其依赖项的版本,则会更快。


推荐