2016-04-14 157 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"/> 
相關問題