spring-redis-data 1.x 和2.x 配置

两个版本的配置有些许区别

1.x 配置

jedis 2.9 、spring-data-redis 1.8 spring 4.x

Redis.xml

// An highlighted block
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
    <!--Spring 与 Redis 分两部分:Redis 自身的连接池、Spring提供数据源接口提供属性接收Redis的连接池-->
    <!--********************************主要配置*********************************************************-->
    <!--引入数据库资源文件:-->
    <context:property-placeholder location="classpath*:proper/redis.properties"  ignore-unresolvable="true"/>
    <!--设置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> 
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
        <!-- IP地址 -->
        <property name="hostName" value="${redis.host}" />
        <!-- 端口号 -->
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.pwd}"></property>
        <!-- 超时时间 默认2000-->
        <property name="timeout" value="${redis.timeout}" />
        <property name="database" value="${redis.dbIndex}"/>
        <!-- 连接池配置引用 -->
        <property name="poolConfig" ref="jedisPoolConfig" />
        <!-- usePool:是否使用连接池 -->
        <property name="usePool" value="${redis.usePool}"/>
    </bean>

    <!--序列化方式-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <!--关联链接工厂-->
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="enableTransactionSupport" value="false"/><!--事务支持,默认关闭-->
        <property name="exposeConnection" value="false"/><!--显示链接数,默认关闭-->
        <property name="enableDefaultSerializer" value="true" /><!--序列化器,默认启动-->
        <!--指定k/v 的序列化-->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>

    <!--********************************主要配置*********************************************************-->

</beans>

2.x 配置

jedis 2.9 、spring-data-redis 2.0 spring 5.x

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
    <!--Spring 与 Redis 分两部分:Redis 自身的连接池、Spring提供数据源接口提供属性接收Redis的连接池-->
    <!--********************************主要配置*********************************************************-->
    <!--引入数据库资源文件:-->
    <context:property-placeholder location="classpath*:proper/redis.properties"  ignore-unresolvable="true"/> 
    
    <!--&lt;!&ndash;Spring-data-redis 2.0之后需要单独 使用RedisPassword输入密码&ndash;&gt;-->
   <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">-->
       <constructor-arg name="thePassword" value="${redis.pwd}"></constructor-arg>
   </bean>

    <!-- 单机模式-->
   <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">-->
        <!--<property name="database" value="${redis.database}"/>-->
        <!--<property name="hostName" value="${redis.host}"/>-->
        <!--<property name="password" ref="redisPassword"/>-->
        <!--<property name="port" value="${redis.port}" />-->
    </bean>

    <!--
    Reids链接工厂
    Spring-data-redis2.0之后推荐使用的构造方法分三种类型:
                RedisStandaloneConfiguration:单机模式(当前使用这种)
                RedisSentinelConfiguration: 哨兵模式
                RedisClusterConfiguration: 集群模式
    -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--采用构造器模式创建 并关联redisStandaloneConfiguration与-->
        <!--提示:Spring-data-redis2.0 配合spring 5使用。并且配置参数使用<constructor-arg>标签-->
       <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
    </bean>

    <!--序列化方式-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <!--关联链接工厂-->
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="enableTransactionSupport" value="false"/><!--事务支持,默认关闭-->
        <property name="exposeConnection" value="false"/><!--显示链接数,默认关闭-->
        <property name="enableDefaultSerializer" value="true" /><!--序列化器,默认启动-->
        <!--指定k/v 的序列化-->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>

    <!--********************************主要配置*********************************************************-->

</beans>

总结:两个版本主要是JedisConnectionFactory 的构造不同。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值