新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正
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...
文章结束给大家分享下程序员的一些笑话语录:
手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn