1、jar包导入
采用maven的方式
org.springframework.data
spring-data-redis
1.4.0.RELEASE
redis.clients
jedis
2.5.0
2.spring与redis配置
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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!--最大空闲连接数-->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!--初始化连接数-->
<property name="minIdle" value="${redis.pool.minIdle}"/>
<!--最大等待时间-->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!--对拿到的connection进行validateObject校验-->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<!--在进行returnObject对返回的connection进行validateObject校验-->
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<!--定时对线程池中空闲的链接进行validateObject校验-->
<property name="testWhileIdle" value="false" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.pass}"
p:pool-config-ref="poolConfig"/>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="poolConfig"/>
<constructor-arg index="1" value="${redis.host}"/>
<constructor-arg index="2" value="${redis.port}" type="int"/>
<constructor-arg index="3"