5、redis之使用spring集成commons-pool

添加spring的依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yzl</groupId>
    <artifactId>redis.first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>

增加spring配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="http://www.springframework.org/schema/security"
 4     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 8        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
 9        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
10        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
11        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
12     
13     <context:property-placeholder location="classpath:redis-pool.properties"/>
14     
15     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
16     <bean class="redis.clients.jedis.JedisPool">
17         <constructor-arg index="0" ref="jedisPoolConfig" />
18         <constructor-arg index="1" value="${redis.ip}" />
19         <constructor-arg index="2" value="${redis.port}" />
20     </bean>
21 </beans>

编写测试类:

 1 package com.yzl;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 import redis.clients.jedis.Jedis;
10 import redis.clients.jedis.JedisPool;
11 
12 /**
13  * RedisApp之spring的测试类
14  *
15  * @author yangzhilong
16  * @see [相关类/方法](可选)
17  * @since [产品/模块版本] (可选)
18  */
19 public class RedisApp2Test {
20     private JedisPool pool;
21     private ApplicationContext app;
22     
23     @Before
24     public void before(){
25         app = new ClassPathXmlApplicationContext("spring-config.xml");
26         pool = app.getBean(JedisPool.class);
27     }
28     
29     @Test
30     public void test(){
31         Jedis jedis = pool.getResource();
32         jedis.set("name", "hello");
33         
34         String value = jedis.get("name");
35         System.out.println("get value :" + value);
36         
37         pool.returnResourceObject(jedis);
38     }
39     
40     @After
41     public void after(){
42         System.out.println("end~~~");
43     }
44 }

测试结果:

get value :hello
end~~~

 

下一篇:6、redis之使用spring-data-redis的Template

posted @ 2015-08-14 14:17  自行车上的程序员  阅读(2474)  评论(0编辑  收藏  举报