0

I am using Spring Data Redis but the official docs confused me:

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

the redisTemplate was injected into ListOperations:

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

but the RedisTemplate didn't extend the ListOperations:

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

I am wondering how is redisTemplate injected into XXXOperations.

my spring-data-redis version is 1.8.1.RELEASE jedis version is 2.9.0

S.I.
  • 3,250
  • 12
  • 48
  • 77
xuhang
  • 1
  • 2
  • Does this answer your question? [Why a "RedisTemplate" can convert to a "ListOperations"](https://stackoverflow.com/questions/43006197/why-a-redistemplate-can-convert-to-a-listoperations) – pringi Oct 13 '20 at 09:22

1 Answers1

0

look at this class 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);
    }
}

}

Use PropertyEditorSupport to transform type (RedisTemplate -> ListOperations)

andy.hu
  • 325
  • 1
  • 2
  • 10