Spring-Data-Redis配置详解

Spring Data Redis的配置网上一大堆,不同的资料可能方法略有出入。这里笔者就记录一下自己亲配的流程吧。


首先我项目中使用了Maven。

第一步,先加个repository:

<repository>
<id>maven-central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>


第二步,改pom.xml,这里的配置不同的version可能会有jar包冲突,这个也要看自身的Spring的版本,笔者Spring3.1X,配的差不多是最新的spring-data-redis和jedis,幸运地通过了:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>


第三步,加配置文件,笔者新建配置文件命名为redis-config.xml,然后:

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


<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean> 


<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"></property>
        <property name="port" value="6379"></property>
        <property name="password" value="123456"></property>
        <property name="timeout" value="10000"></property>
        <property name="usePool" value="true"></property>
        <property name="poolConfig" ref="jedisPoolConfig"></property>
    </bean>


<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>  
        <property name="keySerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>  
        </property>  
        <property name="valueSerializer">  
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>  
        </property>  
    </bean>  
</beans>


这里配的jedisPoolConfig依赖于pom.xml中的jedis,redisTemplate和jedisConnectionFactory依赖于pom.xml中配的spring-data-redis。当然,jedisConnectionFactory默认也是以redis.clients.jedis.JedisPoolConfig作为连接池类。这里显式配置,是为自定义JedisPoolConfig的连接池属性留的,可以用<property>标签做一些个性化的配置,当然这里没有配,采用的是默认配置了。


接下来不打算自己手写,因几篇还算可以的资料来补全此文吧:

1、http://www.360doc.com/content/14/0425/13/203871_372092436.shtml此文对一些自定义配置做了讲解,对使用方法也做了补充

2、http://www.tuicool.com/articles/7Bni6f此文纯手工不用Maven配,也做了一些不错的介绍补充

3、http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html此文对连接池方面做了补充介绍

4、http://spring.io/docs这是官方网站,官方文档,非常使用的

5、http://redisdoc.com/这是redis命令参考手册,不错的~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值