我有多个带有不同序列化程序的Spring Redis模板。我可以为两者使用相同的JedisConnectionFactory实例吗?
答案 0 :(得分:2)
是的,通过在Redis模板的bean定义中指定JedisConnectionFactory
属性,可以对多个Spring Redis模板使用相同的connectionFactory
。
示例:
<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>