首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用spring-data-redis连接多个redis实例

如何用spring-data-redis连接多个redis实例
EN

Stack Overflow用户
提问于 2016-04-14 10:59:49
回答 1查看 5.4K关注 0票数 1

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

我总是收到这个错误:

包装: org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建名为'redisTemplate‘的类路径资源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

你能给我一点提示,说明如何实现这一点吗?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2016-09-30 15:00:27

问题是将connectionFactory提取为bean。如果您在模板bean中声明它,它将正常工作。以下几点对我来说是可行的:

代码语言:javascript
复制
<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"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36621090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档