简体   繁体   中英

How to use Spring cache Manager with redis 1.6.2.RELEASE

We are using Spring Cache Manager with spring-data-redis 1.5.2 . These days we want to upgrade spring-data-redis to latest release ie:1.6.2.RELEASE.

For some weird reason everything works good with 1.5.2 but when upgrading to 1.6.2 we get

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in ServletContext resource [/WEB-INF/spring-cache.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.redis.core.RedisOperations]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

This message seems like a mistake as redisTemplate is RedisTemplate which implements RedisOperations .

Any idea how to solve it?

PS

note that when removing the cache configuration the 1.6.2 version seems to work good. So the issue is with the cache.

Configuration

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring-redis.xml
    /WEB-INF/spring-cache.xml 
    </param-value>
</context-param>

spring-redis.xml

<context:annotation-config />
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

    <bean
        class="org.springframework.security.web.session.HttpSessionEventPublisher" />

    <!-- end of seesion managment configuration -->

    <bean id="redisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="port" value="${app.redis.port}" />
        <property name="hostName" value="${app.redis.hostname}" />
        <property name="password" value="${app.redis.password}" />
        <property name="usePool" value="true" />
    </bean>

    <!-- for publishing string over redis channels -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
    </bean>

    <!-- for storing object into redis key,value -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
    </bean>

spring-cache.xml

<!-- This file must load after spring-redis.xml -->
 <cache:annotation-driven /> 

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

It seems the reason for this bug is that RedisCacheManager has two constructors . Both of them has RedisOperations as parameter. Forsome reason Spring couldnot understand its related to the first constructor and not to the second one. a work around is mention constructor-arg index

<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg index="0" ref="redisTemplate"></constructor-arg>
</bean>

While upgrading from Spring Data Redis 1.5.2.RELEASE to 1.6.2.RELEASE, we need to use the below config for RedisCacheManager. Previous releases were using redis-template-ref instead of redis-operations-ref .

<beans:bean id='cacheManager'
        class='org.springframework.data.redis.cache.RedisCacheManager'
        c:redis-operations-ref='redisTemplate'>
</beans:bean>

This is an old question, but for those who reach this page.

 <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" factory-method="create" c:connection-factory-ref="jedisConnectionFactory" p:transaction-aware="true" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" /> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${cache.redis.host}" p:port="${cache.redis.port}" p:use-pool="true"> <constructor-arg ref="jedisPoolConfig"></constructor-arg> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:maxTotal="${cache.redis.pool.maxTotal}" p:maxIdle="${cache.redis.pool.maxIdle}" p:maxWaitMillis="${cache.redis.pool.maxWaitMillis}" p:testOnBorrow="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM