2016-04-14 86 views
0

springbootアプリケーションを2つの異なるredisインスタンスに接続しようとしています.1つはデータベースとして使用され、もう1つはキャッシュとしてのみ使用されます。 別の名前の接続ファクトリと赤いテンプレートを追加しました。それらをリンクするために@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

バージョンの問題ではありません。私は、1.2.5よりも高いバージョンのspringbootで試してみました。 私は、Springのコンテキストでredis接続ファクトリをBeanとして宣言しないことで解決策を見つけました。私はredisインスタンスごとにRedisTemplateだけを公開しました。このようにして、redis実装は独自の自動構成を開始しますが、他の接続ファクトリは見つかりません。 2つの異なるRedisConnectionFactoryインスタンスで@Bean Beanとして公開すると、問題は解決しません。 –

答えて

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"/> 
関連する問題