Configure Spring configuration file applicationcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/s Pring-mvc-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema /aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd "><!--Connection Pool Configuration - <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"> <!--Maximum number of connections - < Propertyname= "Maxtotal"value= "+" /> <!--Maximum number of idle connections - < Propertyname= "Maxidle"value= "Ten" /> <!--maximum number of connections per release - < Propertyname= "Numtestsperevictionrun"value= "1024x768" /> <!--scan interval for free connections (MS) - < Propertyname= "Timebetweenevictionrunsmillis"value= "30000" /> <!--Connect minimum idle time - < Propertyname= "Minevictableidletimemillis"value= "1800000" /> <!--How long the connection is free and released when idle time > This value and free connections > maximum idle connections are released directly - < Propertyname= "Softminevictableidletimemillis"value= "10000" /> <!--maximum wait milliseconds to get a connection, less than 0: blocking indeterminate time, default-1 - < Propertyname= "Maxwaitmillis"value= " the" /> <!--check validity when getting a connection, default false - < Propertyname= "Testonborrow"value= "true" /> <!--check validity on idle, default false - < Propertyname= "Testwhileidle"value= "true" /> <!--If the connection is exhausted, false to report an exception, ture block until timeout, default true - < Propertyname= "blockwhenexhausted"value= "false" /> </Bean> <!--Redis stand-alone through connection pooling - <BeanID= "Jedispool"class= "Redis.clients.jedis.JedisPool"Destroy-method= "Close"> <Constructor-argname= "Poolconfig"ref= "Jedispoolconfig"/> <Constructor-argname= "Host"value= "192.168.25.145"/> <Constructor-argname= "Port"value= "6379"/> </Bean>
Test:
1 PrivateApplicationContext ApplicationContext;2 3 @Before4 Public voidinit () {5ApplicationContext =NewClasspathxmlapplicationcontext (6"Classpath:applicationContext.xml");7 }8 9 @TestTen Public voidTestjedispool () { OneJedispool pool = (jedispool) applicationcontext.getbean ("Jedispool"); A Try { -Jedis =Pool.getresource (); - theJedis.set ("name", "Lisi"); -String name = Jedis.get ("name"); - System.out.println (name); -}Catch(Exception ex) { + ex.printstacktrace (); -}finally{ + if(Jedis! =NULL){ A //Close Connection at jedis.close (); - } - } -}
Redis and Spring Integration ·