Spring Data Redis 如何将 RedisTemplate 注入为 ListOperations 类型
2020/3/14 5:01:33
本文主要是介绍Spring Data Redis 如何将 RedisTemplate 注入为 ListOperations 类型,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
问题
在 Spring Data Redis 官方文档中,可以看到这样一个常规用法:
<?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:template
RedisTemplate
和 ListOperations
并没有继承关系,这里是怎么将 RedisTemplate
注入到 ListOperations
类型上去的呢?而且不但可以将 RedisTemplate
注入到 ListOperations
,也可以注入到 ValueOperations
、SetOperations
、ZSetOperations
、HashOperations
等类型上。
PropertyEditor
Spring 框架可以通过 java.beans.PropertyEditor
接口的实现类来实现类型转换。
Spring Data Redis 提供了 ListOperationsEditor
可以将 RedisTemplate
转为 ListOperations
:
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); } } }
以上代码中,RedisOperations
是 RedisTemplate
的父级接口,((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.
文档中提到,如果 PropertyEditor
类与它们处理的类在同一个包中,并且类名再加上 Editor
后缀,则无需显式注册,该 PropertyEditor
可以被自动发现。
在 Spring Data Redis 源码中可以看到,ListOperations
类和 ListOperationsEditor
都在 org.springframework.data.redis.core
包下,且 ListOperationsEditor
符合命名规则,即在 ListOperations
类名上加上 Editor
后缀,所以可以自动发现并生效。
关注我
这篇关于Spring Data Redis 如何将 RedisTemplate 注入为 ListOperations 类型的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28微服务架构中API版本控制的实践
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南