spring-data-redis 1.7.2 Внедрение redisTemplate не удалось

Когда я использую 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;
}

Моя версия весенних данных Redis — 1.7.2, а версия Redis — 2.8.1.


person web david    schedule 21.08.2016    source источник
comment
Обновите Spring Core Framework как минимум до версии 4.2.1. Spring Data Redis использует более новый конструктор, см. здесь   -  person mp911de    schedule 21.08.2016
comment
Перешиваю на 4.3.2, работает хорошо, спасибо.^_^   -  person web david    schedule 22.08.2016
comment
@webdavid Как настроить вышеуказанное как XML? У меня та же проблема, что и описанная в разделе Использование Spring Data Redis 1.7.2.RELEASE и Spring 4.3.2. Как настроить RedisCacheManager для той же версии?   -  person nijogeorgep    schedule 29.09.2016


Ответы (2)


Используйте приведенную ниже конфигурацию для использования с 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>
person nijogeorgep    schedule 29.09.2016
comment
Ницца. Спасибо. Но мне пришлось изменить версию Spring. - person cdesmetz; 20.10.2016

Мне нужно только изменить версию Spring на pom проекта без изменения определения 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" />

пом.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>
person cdesmetz    schedule 21.10.2016