1

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

我总是收到这个错误:

包装者:org.springframework.beans.factory.UnsatisfiedDependencyException:在类路径资源 [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class] 中定义名称为“redisTemplate”的 bean 创建错误:通过不满足的依赖关系表示[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,

你能给我任何关于如何实现这一点的提示吗?

提前致谢!

4

1 回答 1

2

问题是将 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"/>
于 2016-09-30T15:00:27.150 回答