With the latest Jedis-2.6.2.jar This bag, this is a little different from the previous one. You will also need to add Spring-data-redis-1.2.1.release.jar and Commons-pool2-2.3.jar.
Create a Spring-redis-config.xml file under the Classpath
<?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 "> <Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"value= "Classpath:redis.properties"/> </Bean> <BeanID= "Poolconfig"class= "Redis.clients.jedis.JedisPoolConfig"> < Propertyname= "Maxidle"value= "+" /> < Propertyname= "Maxtotal"value= "+" /> < Propertyname= "Maxwaitmillis"value= "+" /> < Propertyname= "Testonborrow"value= "true" /> </Bean> <BeanID= "ConnectionFactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory"P:host-name= "localhost"P:port= "6379"P:password=""P:pool-config-ref= "Poolconfig"/> <BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.StringRedisTemplate"> < Propertyname= "ConnectionFactory"ref= "ConnectionFactory" /> </Bean> </Beans>
Because the reference configuration file, cannot use the expression, here writes dead. Using an expression to start an error, I don't know why.
# #redis. Host=localhost # #redis. port=6379## redis.pass= # #redis. maxidle=300## redis.maxtotal= 512# #redis. maxwaitmillis=1000 # #redis. testonborrow=true
Redis.properties file configuration.
The previous version should have configuration redis.maxactive but see the source code, there is no setmaxactive method, so can not inject, instead of redis.maxtotal. Because it's been a long time.
Configuring the Spring-redis-config.xml file in Web. xml
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value> <!--multiple configurations, separated -Classpath*:spring-redis-config.xml</Param-value></Context-param>
Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.RedisTemplate;ImportOrg.springframework.data.redis.serializer.RedisSerializer; Public Abstract classAbstractbaseredisdao<v, k>{@AutowiredprotectedRedistemplate<k, v>redistemplate; Public voidSetredistemplate (Redistemplate<k, v>redistemplate) { This. redistemplate =redistemplate; } /*** Get Redisserializer * <br>------------------------------<br>*/ protectedRedisserializer<string>Getredisserializer () {returnRedistemplate.getstringserializer (); } }
Create an abstract class, and then let the class that you use inherit this method.
@Service ("Arearedisservice") Public classArearedisservice<v, k>extendsAbstractbaseredisdao<v, k> { Public BooleanAddFinalArea Area ) { Booleanresult = Redistemplate.execute (NewRediscallback<boolean>() { PublicBoolean Doinredis (redisconnection connection)throwsDataAccessException {Redisserializer<String> serializer =Getredisserializer (); byte[] key = Serializer.serialize (Area.getid () + ""); byte[] name =serializer.serialize (Area.getname ()); returnconnection.setnx (key, name); } }); returnresult; } PublicArea get (FinalString keyId) {Area result= Redistemplate.execute (NewRediscallback<area>() { PublicArea Doinredis (redisconnection connection)throwsDataAccessException {Redisserializer<String> serializer =Getredisserializer (); byte[] key =serializer.serialize (keyId); byte[] Value =Connection.get (key); if(Value = =NULL) { return NULL; } String name=serializer.deserialize (value); return NewArea (integer.valueof (keyId), name,NULL); } }); returnresult; } }
@Autowired Arearedisservice<?,? >Arearedisservice; PrivateString Path = "/web-inf/jsp/"; @RequestMapping ("/area/redis.htm") PublicModelandview Arearedis (httpservletrequest request, HttpServletResponse response,string name)throwsException {modelandview mv=NewModelandview (path+ "add.html"); Area Area=NewArea (); Area.setcreatetime (NewDate ()); Area.setcommon (true); Area.setdeletestatus (false); Area.setlevel (4); Area.setname (name); Area.setparentid (NULL); Area.setsequence (1); Area.setid (1); Arearedisservice.add (area); Mv.addobject ("Area", area); Mv.addobject ("Arearedis", Arearedisservice.get (Area.getid () + "")); returnMV; }
This is the controller's method, where spring annotations are used.
Using annotations, you need to add <context:component-scan base-package= "Base package path" to the Spring-redis-config.xml file above/> Configure the scan path can not be configured < Context:annotation-config/>, because the front contains the following, he will activate @controller, @Service, @Autowired, @Resource, @Component and other annotations.
Spring Integrated Redis