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

來源:互聯網
上載者:User

標籤:

添加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.xsd10        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd11        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 yangzhilong16  * @see [相關類/方法](可選)17  * @since [產品/模組版本] (可選)18  */19 public class RedisApp2Test {20     private JedisPool pool;21     private ApplicationContext app;22     23     @Before24     public void before(){25         app = new ClassPathXmlApplicationContext("spring-config.xml");26         pool = app.getBean(JedisPool.class);27     }28     29     @Test30     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     @After41     public void after(){42         System.out.println("end~~~");43     }44 }

測試結果:

get value :helloend~~~

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.