Add a dependency on spring
<Projectxmlns= "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>
Add Spring configuration file
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "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.xsd8 Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd9 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/ Spring-security.xsdTen HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd One http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> A - <Context:property-placeholder Location= "Classpath:redis-pool.properties"/> - the <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"></Bean> - <Beanclass= "Redis.clients.jedis.JedisPool"> - <Constructor-argIndex= "0"ref= "Jedispoolconfig" /> - <Constructor-argIndex= "1"value= "${redis.ip}" /> + <Constructor-argIndex= "2"value= "${redis.port}" /> - </Bean> + </Beans>
To write a test class:
1 PackageCom.yzl;2 3 ImportOrg.junit.After;4 ImportOrg.junit.Before;5 Importorg.junit.Test;6 ImportOrg.springframework.context.ApplicationContext;7 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;8 9 ImportRedis.clients.jedis.Jedis;Ten ImportRedis.clients.jedis.JedisPool; One A /** - * Redisapp's Spring test class - * the * @authorYangzhilong - * @see[Related Classes/methods] (optional) - * @since[Product/module version] (optional) - */ + Public classRedisapp2test { - PrivateJedispool Pool; + PrivateApplicationContext app; A at @Before - Public voidbefore () { -App =NewClasspathxmlapplicationcontext ("Spring-config.xml"); -Pool = App.getbean (Jedispool.class); - } - in @Test - Public voidTest () { toJedis Jedis =Pool.getresource (); +Jedis.set ("name", "Hello"); - theString value = Jedis.get ("name"); *System.out.println ("Get Value:" +value); $ Panax Notoginseng Pool.returnresourceobject (Jedis); - } the + @After A Public voidAfter () { theSystem.out.println ("end~~~"); + } -}
Test results:
get Value:helloend~ ~ ~
3. Use of Redis Commons-pool using spring