java-如何在Redis 1.6.2.上使用Spring缓存管理器
作者:互联网
我们正在使用Spring Cache Manager和spring-data-redis 1.5.2.这些天,我们想将spring-data-redis升级到最新版本,即:1.6.2.RELEASE.
出于某种奇怪的原因,一切都适用于1.5.2,但是升级到1.6.2后,
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?
此消息似乎是一个错误,因为redisTemplate是实现RedisOperations的RedisTemplate.
知道如何解决吗?
压力
请注意,在删除缓存配置时,1.6.2版本似乎运行良好.所以问题出在缓存上.
组态
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>
解决方法:
该错误的原因似乎是RedisCacheManager具有两个构造函数.他们两个都有RedisOperations作为参数.由于某种原因,Spring无法理解其与第一个构造函数而不是第二个构造函数的关系.解决方法是提及构造函数-arg索引
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate"></constructor-arg>
</bean>
标签:spring-cache,spring-data-redis,spring-data,spring,java 来源: https://codeday.me/bug/20191027/1945226.html