spring与redis集成有两种,一种是通过spring-data来集成,还有一种是通过 jedis,第一种集成比较简单,第二种自由度更大,本文用的是第一种方式
除spring的jar包,还需要的依赖
- pom.xml
<!--redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
<!-- spring-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
spring集成redis的配置
- spring-context-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载资源文件 -->
<context:property-placeholder
location="classpath*:redis.properties" file-encoding="UTF-8" ignore-unresolvable="true" />
<!--扫描bean-->
<context:component-scan base-package="xyz.mrwood.spring.integration.example.redis" />
<!-- redis配置bean -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!--redis连接类-->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--泛型redis模板类-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- 字符型redis模板类 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
配置文件
- redis.properties
########### reids配置 ############
#ip
redis.host=127.0.0.1
#port
redis.port=6379
#密码
#redis.pass=myredis123
#最大能够保持idel状态的对象数
redis.maxIdle=200
#最大分配的对象数
redis.maxTotal=512
#当池内没有返回对象时,最大等待时间
redis.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.testOnBorrow=true
########### reids配置 ############
测试代码
/**
* Copyright (c) 2016, 791650277@qq.com(Mr.kiwi) All Rights Reserved.
*/
package xyz.mrwood.spring.integration.example.redis;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* 项目:spring-integration-example
* 包名:xyz.mrwood.spring.integration.example.redis
* 功能:
* 作者:Mr.Kiwi
*/
public class TestRedis {
private ApplicationContext applicationContext = null;
@BeforeMethod
public void loadApplication(){
applicationContext =
new ClassPathXmlApplicationContext("spring-context-redis.xml");
}
@Test
public void testOpsForValue(){
// 取出模板类
StringRedisTemplate stringRedisTemplate =
applicationContext.getBean("stringRedisTemplate", StringRedisTemplate.class);
// 设值
stringRedisTemplate.opsForValue().set("name", "kiwi");
// 取值
String name = stringRedisTemplate.opsForValue().get("name");
System.err.println(name);
}
@Test
public void testExpire(){
// 取出模板类
StringRedisTemplate stringRedisTemplate =
applicationContext.getBean("stringRedisTemplate", StringRedisTemplate.class);
// stringRedisTemplate.expire("name", 10, TimeUnit.SECONDS);
Long expireTime = stringRedisTemplate.getExpire("name");
System.err.println(expireTime);
System.err.println(stringRedisTemplate.opsForValue().get("name"));
}
}
项目源码
spring与redis集成