如何使用spring数据(redis)将RedisTemplate注入XXOperations(HashOperations等)

时间:2017-06-16 03:37:16

标签: spring inject jedis spring-data-redis

我使用的是Spring Data Redis,但官方文档让我很困惑:

    <!-- redis template definition -->
    <bean id="redisTemplate" 
        class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory"/>

redisTemplate被注入ListOperations

// inject the template as ListOperations
  @Resource(name="redisTemplate")
  private ListOperations<String, String> listOps;

但RedisTemplate没有扩展ListOperations:

- public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V>, BeanClassLoaderAware
- public class RedisAccessor implements InitializingBean

我想知道如何将redisTemplate注入到XXXOperations中。

我的spring-data-redis版本是1.8.1.RELEASE jedis版本是2.9.0

1 个答案:

答案 0 :(得分:0)

查看此课程ListOperationsEditor

class ListOperationsEditor extends PropertyEditorSupport {

public void setValue(Object value) {
    if (value instanceof RedisOperations) {
        super.setValue(((RedisOperations) value).opsForList());
    } else {
        throw new java.lang.IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
    }
}

}

使用PropertyEditorSupport转换类型(RedisTemplate - &gt; ListOperations)