Spring Data Redis 1.7.2注入redisTemplate失败

5
当我使用Spring Data Redis注入redisTemplate时,出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in com.worktime.configure.JpaConfigurationTest: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
Caused by: java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.<init>(JdkSerializationRedisSerializer.java:53)
at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet(RedisTemplate.java:117)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 40 more

这是我的代码:

@Bean
public RedisConnectionFactory jedisConnectionFactory() {
    nodes = new ArrayList<String>();
    nodes.add("10.10.13.174:7001");
    nodes.add("10.10.13.174:7002");
    nodes.add("10.10.13.174:7003");
    RedisClusterConfiguration conf = new RedisClusterConfiguration(nodes);
    conf.setMaxRedirects(1000);
    JedisConnectionFactory factory = new JedisConnectionFactory(conf);

    return factory;
}

@Bean
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

我的Spring Data Redis版本是1.7.2,Redis版本是2.8.1。


1
将Spring Core Framework升级至4.2.1或更高版本。Spring Data Redis使用较新的构造函数,请参见此处 - mp911de
我升级到了4.3.2版本,现在运行得很好,谢谢!^_^ - web david
@WebDavid 如何将以上内容配置为XML?我遇到了与描述相同的问题,使用Spring Data Redis 1.7.2.RELEASE和Spring 4.3.2。如何为相同版本配置RedisCacheManager? - nijogeorgep
2个回答

1
我只需要在项目pom中更改Spring版本,而不更改bean定义。
<bean id="jedisConnectionFactoryStatic"     class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName"   value="#{confMasterRedisStatic.host}" />
    <property name="port"       value="#{confMasterRedisStatic.port}" />
    <property name="usePool"    value="true" />
</bean>

<bean id="redisTemplateStatic" class="org.springframework.data.redis.core.StringRedisTemplate"
        p:connection-factory-ref="jedisConnectionFactoryStatic" />

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- <spring.version>3.0.7.RELEASE</spring.version> -->
    <spring.version>4.3.3.RELEASE</spring.version>
</properties>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.4.RELEASE</version>
    <!-- <version>1.5.2.RELEASE</version> -->
</dependency>

1
使用以下配置与Spring Data Redis 1.7.2.RELEASE一起使用,这不会引起任何注入问题。
    <cache:annotation-driven cache-manager="redisCacheManager" />

    <!-- Redis Connection Factory -->
    <beans:bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.host-name}" p:port="${redis.port}" p:use-pool="true" />

    <!-- Redis Template Definition -->
    <beans:bean id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"
        p:hashKeySerializer-ref="stringRedisSerializer" />

    <beans:bean id="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <!-- declare Redis Cache Manager -->
    <beans:bean id='redisCacheManager'
        class='org.springframework.data.redis.cache.RedisCacheManager'
        c:redis-operations-ref='redisTemplate'>
    </beans:bean>

好的,谢谢。但我必须更改Spring版本。 - cdesmetz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接