Spring Data Redis实战之提供RedisTemplate
来源:互联网 发布:模拟弹钢琴的软件 编辑:程序博客网 时间:2024/05/19 14:17
参考:
http://www.cnblogs.com/edwinchen/p/3816938.html
本项目创建的是Maven项目
一、pom.xml引入dependencies
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.3.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> <exclusions> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency>
二、配置applicationContext-redis.xml
<?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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> <property name="testOnReturn" value="${redis.pool.testOnReturn}" /> <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.password}" p:poolConfig-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean></beans>
注意:配置poolConfig中property 的name 需要对应JedisPoolConfig这个类中实际的属性;
配置connectionFactory同样要注意这个,因为其他博文配置属性时有所不同,主要是因为redis版本不同引起的有所不同。
三、properties文件配置值
# poolConfig 配置信息redis.pool.maxTotal=1024redis.pool.maxIdle=200redis.pool.maxWaitMillis=1000redis.pool.testOnBorrow=trueredis.pool.testOnReturn=trueredis.pool.testWhileIdle=true# connectionFactory 配置信息redis.host=localhostredis.port=6379redis.timeout=15000redis.password=123456
另外,需要注意的是,要引入该文件可以在applicationContext-redis.xml加入
<!-- scanner redis properties 假如有封装用于加载properties,就不需要加这句了 --><context:property-placeholder location="classpath:property/redis.properties" />
四、使用RedisTemplate
其他项目需要RedisTemplate的时候,需要引入以上maven模块的dependency
测试类使用:
/** * 测试 spring-data-redis 集成 * Created by zhile on 2017/5/12 0012. */@RunWith(SpringRunner.class)@SpringBootTestpublic class TestRedis { @Autowired private RedisTemplate redisTemplate; @Test public void testTemple() { ValueOperations valueOperations = redisTemplate.opsForValue(); valueOperations.set("spring-data-redis::test", "tom", 1000, TimeUnit.SECONDS); System.out.println("set successed");/* ValueOperations valueOpers = redisTemplate.opsForValue(); System.out.println("get:" + valueOpers.get("spring-data-redis::test"));*/ } @Test public void testRedisTemple() { ValueOperations valueOpers = redisTemplate.opsForValue(); System.out.println("get:" + valueOpers.get("spring-data-redis::test")); }}
这样,只要引入集成了RedisTemplate的maven模块,就可以直接使用。
五、问题小结:
1.引入的dependency需要注意兼容性,因为redis2.7.3 中的commons-pool2版本不完整,需要引入较新的commons-pool2。
Caused by: java.lang.NoSuchMethodError: redis.clients.jedis.JedisPool.apache/commons/pool2/impl/GenericObjectPoolConfig等等
2.假如spring-data-redis的版本1.8.3,而redis的版本还是为2.7.3的话,会报这样的错:
所以,需要注意版本兼容的问题,这是因为spring-data-redis的版本太新点。
java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [applicationContext-redis.xml]:...Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [applicationContext-redis.xml]:...Caused by: java.lang.NoClassDefFoundError:Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnectionFactory...
补充参考:
http://zhaozhiming.github.io/blog/2015/04/12/spring-data-redis/
http://www.baeldung.com/spring-data-redis-tutorial
阅读全文
0 0
- Spring Data Redis实战之提供RedisTemplate
- Spring-Data-Redis之RedisTemplate的使用
- Spring-Data-Redis之RedisTemplate的使用
- spring集成redis之RedisTemplate
- 使用Spring-Data-Redis存储对象(redisTemplate)
- 使用Spring-Data-Redis存储对象(redisTemplate)
- Spring Data Redis(RedisTemplate的使用)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Spring-Data-Redis集群配置和RedisTemplate用法
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- spring data RedisTemplate无效果
- spring-data整合redis提供通用接口
- Spring集成Redis使用RedisTemplate
- MS DOS 常用命令整理
- Git SSH Key 生成步骤
- IntelliJ IDEA 中 Ctrl+Alt+Left/Right 失效
- Java中POJO及其细分XO、DAO的概念
- Spring Boot 系列文章推荐
- Spring Data Redis实战之提供RedisTemplate
- Linux笔记1-简介
- 使用Git上传项目到GitHub
- 七、rabbitMQ Topics
- Linux笔记2-常用命令
- Java 8实战之读书笔记一:内容简介
- JSP中编码小论
- Java 8实战之读书笔记二:基础知识
- 233矩阵(上三角矩阵求累加量//代码和思路不一样