redis与Spring集成

redis与Spring集成比较简单,本文不涉及Spring的一些基本概念,读都需先具备Spring的相关知识。

先在maven中添加依赖

pom.xml

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

applicationContext-redis.xml加入你的Spring context

<?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.0.xsd
          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" />
		<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的类

/**
 * 
 */
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>

注册bean

main函数如下:

/**
 * 
 */
package com.zolcorp.mahout.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

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...




你可能感兴趣的:(redis,spring)