Remember to destroy object Returnbrokenresource when an exception occurs, and then connect Returnresource when you are finished using it.
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:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.x SD Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context . xsd "> <!--<context:property-placeholder location= "classpath:redis.properties"/> - <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"> < Propertyname= "Maxidle"value= "+" /> < Propertyname= "Maxactive"value= "+" /> < Propertyname= "Maxwait"value= "10000" /> < Propertyname= "Testonborrow"value= "true" /> </Bean> <!--<bean id= "ConnectionFactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:host-name=" ${redis.host} "p:port=" ${re Dis.port} "p:password=" ${redis.pass} "p:pool-config-ref=" Poolconfig "/> <bean id=" redisTemplate "class=" O Rg.springframework.data.redis.core.StringRedisTemplate "> <property name=" connectionfactory "ref=" connecti Onfactory "/> </bean> <bean id=" Userdao " class=" Com.x.dao.impl.userdao "/> - <BeanID= "Jedisshardinfo"class= "Redis.clients.jedis.JedisShardInfo"> <Constructor-argIndex= "0"value= "localhost" /> <Constructor-argIndex= "1"value= "6379"type= "int"/> <!--<property name= "password" value= "123456"/> - </Bean> <BeanID= "Shardedjedispool"class= "Redis.clients.jedis.ShardedJedisPool"> <Constructor-argIndex= "0"ref= "Jedispoolconfig" /> <Constructor-argIndex= "1"> <List> <refBean= "Jedisshardinfo" /> </List> </Constructor-arg> </Bean></Beans>
Redis.properties:
# Redis Settings redis.host=localhost redis.port=6379 redis.pass=123456 redis.maxidle=300 redis.maxactive=600 redis.maxwait=1000 redis.testonborrow=true
Test:
PackageCom.redis.redis;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubClasspathxmlapplicationcontext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Shardedjedispool Shardedjedispool= (Shardedjedispool) ctx.getbean ("Shardedjedispool"); Shardedjedis Jedis=Shardedjedispool.getresource (); Jedis.set ("Ke", "Luojie"); System.out.println ("---------------------"); }}
Spring Consolidated Redis