Spring Data Redis实战之提供RedisTemplate

时间:2023-03-10 08:41:20
Spring Data Redis实战之提供RedisTemplate

参考:

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=1024
redis.pool.maxIdle=200
redis.pool.maxWaitMillis=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.pool.testWhileIdle=true # connectionFactory 配置信息
redis.host=localhost
redis.port=6379
redis.timeout=15000
redis.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)
@SpringBootTest
public 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