【问题标题】:Can we use single JedisConnectionFactory instance with multiple spring redis template?我们可以将单个 JedisConnectionFactory 实例与多个 spring redis 模板一起使用吗?
【发布时间】:2015-12-31 02:03:34
【问题描述】:

我有多个带有不同序列化程序的 Spring Redis 模板。我可以为两者使用相同的 JedisConnectionFactory 实例吗?

【问题讨论】:

    标签: spring redis spring-boot jedis spring-data-redis


    【解决方案1】:

    是的,您可以通过在 Redis 模板的 bean 定义中指定 connectionFactory 属性,将相同的 JedisConnectionFactory 与多个 Spring Redis 模板一起使用。

    例子:

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="usePool" value="true"/>
    </bean>
    
    <bean id="redisTemplateOne" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="com.example.KeyOne"/>
        </property>
        <property name="valueSerializer">
            <bean class="com.example.ValueOne"/>
        </property>
    </bean>
    
    <bean id="redisTemplateTwo" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="com.example.KeyTwo"/>
        </property>
        <property name="valueSerializer">
            <bean class="com.example.ValueTwo"/>
        </property>
    </bean>
    

    【讨论】:

    • 我通过点击和试验找到了答案。请也使用 java config 更新您的答案。到时候我会把它标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2012-12-13
    • 2020-01-14
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多