Redis與Spring集成

redis與Spring集成比較簡單,本文不涉及Spring的一些基本概念,讀都需先具有Spring的相關知識。java

先在maven中添加依賴redis

pom.xmlspring

<!-- redis -->    
<dependency>      
   <groupId>redis.clients</groupId>      
   <artifactId>jedis</artifactId>      
   <version>2.0.0</version>    
</dependency>    
<!-- redis -->


applicationContext-redis.xml加入你的Spring contextapp

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxActive" value="50" />    <!--最大鏈接數-->
   <property name="maxIdle" value="10" />     <!--最大空閒數-->
   <property name="maxWait" value="1000" />    <!--最大等待時間ms-->
   <property name="testOnBorrow" value="true" />  
</bean>  
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">    
   <constructor-arg index="0" value="172.21.1.121" />    
   <constructor-arg index="1" value="6379" />  
</bean>  
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">        <constructor-arg index="0" ref="jedisPoolConfig" />    
     <constructor-arg index="1">      
         <list>        
          <ref bean="jedis.shardInfo" />      
         </list>    
     </constructor-arg>  
</bean>
</beans>


RedisDao.java操做redis的類maven

/**
*
*/package com.zolcorp.mahout.dao;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* @author hadoop
*
*/
public class RedisDao {    
  @Autowired  
 private ShardedJedisPool shardedJedisPool;    
 public void set(String key, String value)  {  
 ShardedJedis jedis =  shardedJedisPool.getResource();    
 jedis.set(key, value);  
 }    
 public String get(String key)  {    
 ShardedJedis jedis =  shardedJedisPool.getResource();    
 return jedis.get(key);  
 }
}


其它一些方法,請讀者本身完成。函數

<bean id="redisDao" class="com.zolcorp.mahout.dao.RedisDao"></bean>


註冊beanoop

main函數以下:spa

/** *  */ package com.zolcorp.mahout.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContex; import com.zolcorp.mahout.dao.RedisDao; /** * @author hadoop * */ public class RedisMain {   /**    * @param args    */   public static void main(String[] args) {      ApplicationContext context = new FileSystemXmlApplicationContext(               new String[] { "classpath:applicationContext-bean.xml",                       "classpath:applicationContext-datasource.xml",                       "classpath:applicationContext-redis.xml"});      RedisDao redisDao = context.getBean("redisDao", RedisDao.class);      redisDao.set("test", "test-----ming...");       System.out.println(redisDao.get("test"));  }//test-----ming...}
相關文章
相關標籤/搜索