2016-04-14 155 views
0

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

我总是收到此错误:

Wrapped by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.redis.connection.RedisConnectionFactory]: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: redisCacheFactory,redisJitFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: redisCacheFactory,redisJitFactory

你能给我如何能够实现这个任何暗示?

在此先感谢!

+0

看起来像是遇到了一些版本冲突。您使用哪种版本的Spring Boot和Spring Data Redis? – mp911de

+0

不是版本问题。我试过不同版本的springboot高于1.2.5。 我发现了一个解决方案,在spring的上下文中没有将redis连接工厂声明为bean。我只为每个redis实例公开了RedisTemplate。通过这种方式,redis实现启动自己的自动配置,但没有找到任何其他连接工厂。 如果您使用两个不同的RedisConnectionFactory实例的@Bean作为bean公开,那么问题仍然存在。 –

回答

1

问题是将connectionFactory作为bean提取。如果你在template 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"/> 
相关问题