如何使用spring-data-redis

时间:2016-04-14 10:59:50

标签: spring-boot spring-data-redis

我正在尝试将一个springboot应用程序连接到2个不同的redis实例:一个用作数据库,另一个用作缓存。 我添加了不同的连接工厂和不同名称的redis模板,我使用@Qualifier来链接它们。 我试图从自动配置中禁用类RedisAutoConfiguration,但没有任何作用。

我总是收到这个错误:

  

包装:org.springframework.beans.factory.UnsatisfiedDependencyException:在类路径资源中定义名称为'redisTemplate'的bean时出错[org / springframework / boot / autoconfigure / data / redis / RedisAutoConfiguration $ RedisConfiguration.class]:不满意通过类型为[org.springframework.data.redis.connection.RedisConnectionFactory]的索引0的构造函数参数表示的依赖关系:没有定义类型为[org.springframework.data.redis.connection.RedisConnectionFactory]的限定bean:期望的单个匹配bean但是找到2:redisCacheFactory,redisJitFactory;嵌套异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[org.springframework.data.redis.connection.RedisConnectionFactory]类型的限定bean:期望的单个匹配bean但找到2:redisCacheFactory,redisJitFactory

你能否给我一些关于如何实现这一点的暗示?

提前致谢!

1 个答案:

答案 0 :(得分:1)

问题是将connectionFactory解压缩为bean。如果你在模板bean中声明它正常工作。以下适用于我:

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
    p:defaultSerializer-ref="stringRedisSerializer">
    <property name="connectionFactory">
       <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.ip}" p:port="6379" p:use-pool="true"/>
    </property>
</bean> 

<bean id="redisTemplate2" class="org.springframework.data.redis.core.RedisTemplate"
    p:defaultSerializer-ref="stringRedisSerializer">
    <property name="connectionFactory">
       <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.ip2}" p:port="6379" p:use-pool="true"/>
    </property>
</bean> 

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