关于SSM整合Spring-data-Redis踩的一些坑

一、关于SSM整合Redis需要知道的一些小知识


在学习Redis的时候,大家应该知道,JAVA操作redis通常使用的是Jedis,通过java代码来操作redis的数据存储读取等操作,用过的人应该知道,Jedis客户端已经足够简单和轻量级了,但是呢,在此同时,Spring也为Redis提供了支持,就是在Spring-data模块中的Spring-Data-Redis(SDR),它一部分是基于Jedis客户端的API封装,另一部分是对Spring容器的整合。

大家应该都知道,Spring容器可以作为一个大工厂,为各种对象创建实例,关于SSM与Redis的整合,准确点,其实也就是Spring与Redis的整合。

又说回来了,Spring要和Redis整合需要用到的,就是Spring-Data模块中的Spring-data-Redis。
 

二、SDR资料(Spring-Data-Redis)


资料:http://projects.spring.io/spring-data-redis/ 
文档:https://docs.spring.io/spring-data/redis/docs/2.2.x/reference/html/#reference

对于RedisTemplate类就不解释了,个人觉得没有什么比官方文档说的更为详细的了 ,这里只介绍整合

三、整合步骤

1.导入相应的jar包

    <!-- Redis整合   -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

这里可能会有朋友觉得,为什么spring-data-redis都到2.x版本了,你却还演示1.x版本的,因为2.x版本。。有很大的改动,如JedisConnectionFactory的配置属性过期,启动报错java.lang.ClassNotFoundException: javax.annotation.Nullable等,在整合的时候可能会报错。

网上的资料翻了很多也还没配好,先用1.x版本的学习着吧。

2.配置xml文件

redis.host=192.168.3.143
redis.port=6379
redis.dbIndex=1
redis.expiration=3000
redis.maxIdle=300
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.password=有密码就配,没有就不用

    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" order="2"/>

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

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="database" value="${redis.dbIndex}"/>
        <property name="password" value="${redis.password}"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>

这里注意第一行,引入配置文件的时候记得加上后面的属性,不然会无法解析properties占位符的问题,因为spring只允许有一个<context:property-placeholder/>,如果想加载多个文件,就会报错,后加载的文件无法加载成功

注意:redisTemplate默认情况下采用的jdk的defaultSerializer会进行序列化操作,这样你在插入key的时候和中文value的时候,会看到一串的序列码,这样里将上面的redisTemplate配置改为如下:

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!--   key进行序列化配置,默认JDK改为String     -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>

到这基本就整合成功了,你只要在自己的代码里注入RedisTemplate就可以对redis进行操作啦。

发布了27 篇原创文章 · 获赞 32 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39809458/article/details/93600498