2017-06-16 362 views
0

我使用Spring數據Redis的,但官方的文檔搞糊塗了:RedisTemplate如何注入XXOperations(HashOperations等。)用彈簧數據(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。

我的彈簧數據redis的版本是1.8.1.RELEASE jedis版本是2.9.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 - > ListOperations)