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"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "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:schemalocati on= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.x SD "> <bean class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name= "Locations" value= "classpath:redis.properties"/> </bean> <bean id= "Poolconfig" CLA Ss= "redis.clients.jedis.JedisPoolConfIG "> <property name=" maxidle "value="/> <property name= "maxtotal" value= "/>" <property name= "Maxwaitmillis" value= "$"/> <property name= "Testonborrow" value= "true"/> </bean> <bean id= "ConnectionFactory" class= "Org.springframework.data.redis.connection.jedis.JedisCo Nnectionfactory "p:host-name=" localhost "p:port=" 6379 "p:password=" "p:pool-config-ref=" Poolconfig "/> <bean id= "Redistemplate" class= "Org.springframework.data.redis.core.StringRedisTemplate" > <property Name= "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>
Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.redis.core.redistemplate;import Org.springframework.data.redis.serializer.redisserializer;public abstract class Abstractbaseredisdao<v, K> { @Autowired protected redistemplate<k, v> redistemplate; public void Setredistemplate (redistemplate<k, v> redistemplate) { this.redistemplate = redistemplate; } /** * Get redisserializer * <br>------------------------------<br> */ protected Redisserializer<string> Getredisserializer () { return Redistemplate.getstringserializer (); } }
Create an abstract class, and then let the class that you use inherit this method.
@Service ("Arearedisservice") public class Arearedisservice<v, k> extends Abstractbaseredisdao<v, k> {publi C Boolean Add (Final area area) {Boolean result = Redistemplate.execute (new rediscallback<boolean> () { Public Boolean Doinredis (redisconnection connection) throws DataAccessException {Redisserializer<s Tring> serializer = Getredisserializer (); byte[] key = Serializer.serialize (Area.getid () + ""); byte[] name = Serializer.serialize (Area.getname ()); Return Connection.setnx (key, name); } }); return result; ' Public area get ' (final String keyId) {Area result = Redistemplate.execute (new Rediscallback<area> ;() {public area Doinredis (redisconnection connection) throws Dataaccessexcepti on {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 new Area (integer.valueof (keyId), name,null); } }); return result; } }
@Autowired arearedisservice<?,? > arearedisservice; Private String Path = "/web-inf/jsp/"; @RequestMapping ("/area/redis.htm") public Modelandview Arearedis (httpservletrequest request, HttpServletResponse response,string name) throws Exception { Modelandview mv = new Modelandview (path+ "add.html"); C8/>area area = new Area (); Area.setcreatetime (New Date ()); 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 () + "")); return MV; }
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.
Http://www.cnblogs.com/hjy9420/p/4278002.html
Spring Integrated Redis