1. 程式人生 > >redis與spring整合--不使用spring-data-redis

redis與spring整合--不使用spring-data-redis

個人感覺如果使用spring-data-redis只作為快取的話有點累贅,所以有了以下方式

之前在網上搜索了一些資料,但是其中的配置總是報錯,原因是JedisShardInfo類缺少相應的構造方法......

1.redis.xml配置

spring的配置檔案

<?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: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.xsd  
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">  

    <context:property-placeholder location="classpath:redis.properties" />  

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                	<constructor-arg name="host" value="http://redis:${redis.password}@${redis.host}:${redis.port}"></constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean>
    
    <bean id="data" class="com.x.redis.RedisDataSource">
    	<property name="shardedJedisPool" ref="shardedJedisPool"></property>
    </bean>
    
</beans>

       <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <!-- 這裡的http://redis:並不是固定的,只是為了傳遞引數 -->
                     <constructor-arg name="host" value="http://redis:${redis.password}@${redis.host}:${redis.port}"></constructor-arg>
                </bean>
            </list>
        </constructor-arg>
redis.properties配置
#redis config
redis.host=127.0.0.1
redis.port=6379
redis.password=redis
redis.maxIdle=100 
redis.maxWait=1000 
redis.testOnBorrow=true 
redis.timeout=100000

2.redisDatasource類


為了獲取客戶端例項方便

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisDataSource
{
	private ShardedJedisPool shardedJedisPool;
	
	/**
	 * spring注入shardedJedisPool
	 * @param shardedJedisPool
	 */
	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool)
	{
		this.shardedJedisPool = shardedJedisPool;
	}
	
	/**
	 * 獲取redis客戶端ShardedJedis物件
	 * @return
	 */
	public ShardedJedis getRedisClient(){
		try
		{
			return shardedJedisPool.getResource();
		} catch (Exception e)
		{
			System.err.println("get resource error");
		}
		return null;
	}
}

3.測試程式碼

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.ShardedJedis;

import com.vclouds.redis.RedisDataSource;


public class Sdao
{
	@Test
	public void test(){
		ApplicationContext app = new ClassPathXmlApplicationContext("classpath:redis.xml");
		System.out.println(app);
		ShardedJedis client = app.getBean("data",RedisDataSource.class).getRedisClient();
		System.out.println(client);
		
		client.set("aa", "aaaa");
		System.out.println(client.get("aa"));
	}
	
	
}

4.常見問題

1).程式碼執行時要保證redis的伺服器是啟動狀態,否則無法連線

2).以上注入方式參考JedisShardInfo類原始碼

3).個人感覺以上方式使用起來會便捷一些,但是如果將redis作為一個nosql資料庫的話,這種方式不推薦