【问题标题】:How is RedisTemplate injected into XXOperations(HashOperations ect.) using spring data (redis)RedisTemplate如何使用spring数据(redis)注入XXOperations(HashOperations等)
【发布时间】:2017-06-16 03:37:16
【问题描述】:

我正在使用 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

【问题讨论】:

标签: spring inject jedis spring-data-redis


【解决方案1】:

看看这堂课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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2015-08-07
    • 2017-12-28
    相关资源
    最近更新 更多