jar包准备:jedis-2.8.0.jar
redis.properties配置文件
#redis中心
redis.host=127.0.0.1
redis.port=6379
redis.password=root
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.minIdle=50
redis.testOnBorrow=true
redis.timeout=100000
redis.maxTotal=100
app-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- properties config -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/com/springmvc/config/redis.properties</value>
</list>
</property>
</bean>
<!-- Redis配置 -->
<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal">
<value>${redis.maxTotal}</value>
</property>
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<!-- <property name="testOnReturn" value="${redis.testOnReturn}"/> -->
</bean>
<!-- redis的连接池pool,不是必选项:timeout/password -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
<constructor-arg index="3" value="${redis.timeout}" type="int" />
<!-- <constructor-arg index="4" value="${redis.password}"/> -->
</bean>
</beans>
测试:
/*
* 文件名:RedisTest.java
* 版权:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: RedisTest.java
* 修改人:peiyu
* 修改时间:2016年8月3日
* 修改内容:新增
*/
package com.springmvc.imooc.test;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sun.tools.jar.resources.jar;
/**
*
* @author peiyu
*/
public class RedisTest extends BaseTest {
private ApplicationContext app;
private JedisPool jedisPool;
@Before
public void init() {
app = new ClassPathXmlApplicationContext("com/springmvc/config/app-context.xml");
jedisPool=(JedisPool) app.getBean("jedisPool");
}
@Test
public void test() {
Jedis resource = jedisPool.getResource();
System.out.println("set前name=" + resource.get("name"));
resource.set("name", "zhangsan");
System.out.println("set后name=" + resource.get("name"));
resource.del("name");
System.out.println("delete后name=" + resource.get("name"));
}
}