Don't say more, first put the code link: http://git.oschina.net/alexgaoyh/alexgaoyh
Then the project effect section is as follows:
The data query display graph of Redis in Win32 environment is attached:
Some pom.xml codes are attached below:
<dependency> <groupId>org.springframework</groupId> <artifactid>spring-core</ artifactid> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.4.0.RELEASE</version> </dependency><dependency> <groupId> Redis.clients</groupid> <artifactId>jedis</artifactId> <version>2.6.0</versio n> </dependency><dependency> <groupId>org.slf4j</groupId> <artifa ctid>slf4j-api</artifactid> <version>1.7.0</version> </dependency> < ;d ependency> <groupId>org.slf4j</groupId> <artifactid>jcl-over-slf4j</artifa Ctid> <Version>1.6.1</version> </dependency><dependency><groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId><version>1.7.0</version></dependency>
Redis-config.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:tx=" http://www.springframework.org/ Schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.sprin Gframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd " > <bean id= "poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > </bean> <bean id= "connectionf Actory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:host-name=" ${redis.host } "p:port=" ${redis.port} "p:password=" ${redis.pass} "p:pool-config-ref=" Poolconfig "/> <bean id="Redistemplate" class= "org.springframework.data.redis.core.StringRedisTemplate" > <property name= " ConnectionFactory "ref=" ConnectionFactory "/> </bean> </beans>Redis-config.properties
redis.host=127.0.0.1redis.port=6379redis.pass=
Note: The method of introducing the *.properties file is:
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>classpath:*-config.properties</va Lue> </list> </property></bean>
To create a Java class for redisclient:
Package Com.alexgaoyh.redis.util;import Java.io.serializable;import Javax.annotation.resource;import Org.springframework.dao.dataaccessexception;import org.springframework.data.redis.connection.RedisConnection; Import Org.springframework.data.redis.core.rediscallback;import Org.springframework.data.redis.core.redistemplate;import Org.springframework.data.redis.serializer.redisserializer;import org.springframework.stereotype.component;@ Component ("Redisclient") public class Redisclient<k extends Serializable, V extends serializable> {@Resource (name = "Redistemplate") protected redistemplate<k, v> redistemplate;/** * Add * @param key * @param value * @return */pu Blic Boolean Add (Final string key, final string value) {Boolean Resultboolean = False;if (redistemplate! = null) {Resultboo Lean = Redistemplate.execute (new rediscallback<boolean> () {public Boolean Doinredis (redisconnection connection) Throws DataAccessException {redisserializer<string> serializer = getredIsserializer (); byte[] keys = serializer.serialize (key); byte[] values = serializer.serialize (value); Return connection.setnx (keys, values); } }); }else{system.out.println (redistemplate = = null);} return Resultboolean; }/** * Gets the object by key */public string get (final string key) {string resultstr = null; if (redistemplate! = null) {ResultStr = Redistemplate.execute (new rediscallback<string> () {public String doin Redis (redisconnection connection) throws DataAccessException {redisserializer<string> serializer = getred Isserializer (); Byte[] keys = serializer.serialize (key); Byte[] values = Connection.get (keys); if (values = = null) {return null; The String value = serializer.deserialize (values); return value; } }); } return resultstr; }/** * Get Redisserializer * */protected redisserializer<string> Getredisserializer () { Return redistemplate.getsTringserializer (); } public redistemplate<k, V> getredistemplate () {return redistemplate;} public void Setredistemplate (redistemplate<k, v> redistemplate) {this.redistemplate = Redistemplate;}}Test method:
@Resourceprivate redisclient<string, string> redisclient; @RequestMapping (value= "login") Public Modelandview login () { //Send mail demo //emailutil.send ("Subject", " Content "," [email protected] "); Redisclient.add ("AAAA", "AAAA"); System.out.println (Redisclient.get ("AAAA")); return new Modelandview ("Views/test"); }
To create a bean using annotations:
<context:component-scan base-package= "Com.alexgaoyh"/>
Reids Win32 Win64 Environment service side
Http://git.oschina.net/alexgaoyh/alexgaoyh/attach_files
Java EE Spring Integration redis2.6