Spring Integrated Redis

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.