码迷,mamicode.com
首页 > 编程语言 > 详细

3、redis之使用commons-pool使用spring

时间:2015-08-14 15:38:27      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

添加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~~~

 

3、redis之使用commons-pool使用spring

标签:

原文地址:http://www.cnblogs.com/yangzhilong/p/4729857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!