Spring Data Redis 如何將 RedisTemplate 注入爲 ListOperations 類型

問題

在 Spring Data Redis 官方文檔中,能夠看到這樣一個常規用法:html

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

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

</beans>
public class Example {

  // inject the actual template
  @Autowired
  private RedisTemplate<String, String> template;

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

  public void addLink(String userId, URL url) {
    listOps.leftPush(userId, url.toExternalForm());
  }
}

代碼摘自:https://docs.spring.io/spring-data/redis/docs/2.2.5.RELEASE/reference/html/#redis:templatejava

RedisTemplateListOperations 並無繼承關係,這裏是怎麼將 RedisTemplate 注入到 ListOperations 類型上去的呢?並且不但能夠將 RedisTemplate 注入到 ListOperations ,也能夠注入到 ValueOperationsSetOperationsZSetOperationsHashOperations 等類型上。git

PropertyEditor

Spring 框架能夠經過 java.beans.PropertyEditor 接口的實現類來實現類型轉換。github

Spring Data Redis 提供了 ListOperationsEditor 能夠將 RedisTemplate 轉爲 ListOperationsredis

class ListOperationsEditor extends PropertyEditorSupport {

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

代碼摘自:https://github.com/spring-projects/spring-data-redis/blob/2.2.5.RELEASE/src/main/java/org/springframework/data/redis/core/ListOperationsEditor.javaspring

以上代碼中,RedisOperationsRedisTemplate 的父級接口,((RedisOperations) value).opsForList() 實際上就是調用 RedisTemplate.opsForList() 獲取 ListOperations微信

Spring 如何註冊 PropertyEditor

Note also that the standard JavaBeans infrastructure automatically discovers PropertyEditor classes (without you having to register them explicitly) if they are in the same package as the class they handle and have the same name as that class, with Editor appended. For example, one could have the following class and package structure, which would be sufficient for the SomethingEditor class to be recognized and used as the PropertyEditor for Something-typed properties.

摘自:https://docs.spring.io/spring/docs/5.2.4.RELEASE/spring-framework-reference/core.html#beans-beans-conversionapp

文檔中提到,若是 PropertyEditor 類與它們處理的類在同一個包中,而且類名再加上 Editor 後綴,則無需顯式註冊,該 PropertyEditor 能夠被自動發現。框架

在 Spring Data Redis 源碼中能夠看到,ListOperations 類和 ListOperationsEditor 都在 org.springframework.data.redis.core 包下,且 ListOperationsEditor 符合命名規則,即在 ListOperations 類名上加上 Editor 後綴,因此能夠自動發現並生效。url

關注我

關注微信公衆號

相關文章
相關標籤/搜索