spring-data-redis2.0以上配置redis连接

 

1.所需的maven依赖

复制代码

     <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>2.0.0.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
      </dependency>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-xxx</artifactId>
    </dependency>

复制代码

 

此处需要注意的是spring-data-redis2.0以上版本所需的jedis版本为2.9.0  目前最新的版本也为2.9.0  否则会报ClassNotFoundException或者NoSuchMethodError等异常

spring-xxx即为spring其他依赖的包,网上很多  找到粘贴吧

此外如果想查看工程用的包所依赖的包的版本可以去http://mvnrepository.com/ 网上输入对应的包如spring-data-redis  

点击后会罗列出maven仓库里所包含的版本  点击当前使用的版本如案例中的2.0.0  往下翻则会展示出所选包依赖的包

 

 这儿就会指明依赖jedis的包且最低版本为2.9.0

 

2.配置redis.properties

复制代码

redis.host = 172.25.12.123
redis.port = 6379
redis.auth = admin
redis.maxTotal = 100
redis.maxIdle = 20
redis.maxWaitMillis = 10000
redis.testOnBorrow = true
redis.testOnReturn = true
redis.dbIndex = 3

复制代码

3.配置spring-redis.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:context="http://www.springframework.org/schema/context"
       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="redis.properties" ignore-unresolvable="true"></context:property-placeholder>
    <!--设置jedisPool链接池的配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
    </bean>
    <!--redis链接密码-->
    <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
        <constructor-arg name="thePassword" value="${redis.auth}"></constructor-arg>
    </bean>

    <!--spring-data-redis2.0以上的配置-->
    <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="password" ref="redisPassword" />
        <property name="database" value="${redis.dbIndex}"/>
    </bean>
    <!--配置jedis链接工厂 spring-data-redis2.0中
        建议改为构造器传入一个RedisStandaloneConfiguration  单机
                            RedisSentinelConfiguration  主从复制
                            RedisClusterConfiguration  集群-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--注销掉的部分为spring-data-redis2.0以下的版本配置的方式-->
      <!--  <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
        <property name="password" value="${redis.auth}" />
        <property name="database" value="${redis.dbIndex}"/>-->
        <!--spring-data-redis2.0以上建议获取的方式-->
        <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
    </bean>

    <!--手动设置 key  与 value的序列化方式-->
    <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    <!--配置jedis模板  -->
    <bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />
    </bean>

    <!--也可以StringRedisTemplate  专注于String的操作  -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <!--<property name="connectionFactory" ref="jedisConnectionFactory"></property>-->
        <!--在StringRedisTemplate与redisTemplate不同,可以直接造构造器中传入ConnectionFactory-->
        <constructor-arg name="connectionFactory" ref="jedisConnectionFactory"></constructor-arg>
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />

    </bean>
</beans>

复制代码

上面需要注意一点的是

在spring-data-redis2.0以上的版本中在配置密码的时候不能像老版本直接设置密码值,需要注入一个RedisPassword的bean  在RedisPassword的构造方法中设置密码

 

3.最后进行测试测试

复制代码

package com.springactul.splx.nosql.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by zhoum on 2018-06-22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-*.xml"})
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void test1(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("first","hellow word");
        System.out.println(valueOperations.get("first"));
    }

    @Test
    public void test2(){
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("secound","hello everyone");
        System.out.println(stringStringValueOperations.get("secound"));
    }
}

复制代码转载:http://www.cnblogs.com/hetutu-5238/p/9212868.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值