spring + redis 配置

该博客介绍了如何在Spring项目中配置Redis,并通过Jedis进行操作。首先,使用Maven引入了spring-data-redis和jedis的相关依赖。接着详细讲解了Spring的redis.xml和redis.properties配置文件设置。最后,提供了Redis工具类的实现,便于开发中的键值对存取。
摘要由CSDN通过智能技术生成

1、jar包导入

采用maven的方式

org.springframework.data
spring-data-redis
1.4.0.RELEASE


redis.clients
jedis
2.5.0

2.spring与redis配置

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:p="http://www.springframework.org/schema/p" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  

    <property name="maxTotal" value="${redis.pool.maxActive}" />
        <!--最大空闲连接数-->
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <!--初始化连接数-->
    <property name="minIdle" value="${redis.pool.minIdle}"/>
        <!--最大等待时间-->
    <property name="maxWaitMillis" value="${redis.pool.maxWait}" />
        <!--对拿到的connection进行validateObject校验-->
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
        <!--在进行returnObject对返回的connection进行validateObject校验-->
    <property name="testOnReturn" value="${redis.pool.testOnReturn}" />
        <!--定时对线程池中空闲的链接进行validateObject校验-->
    <property name="testWhileIdle" value="false" />
  </bean>  
  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
    p:host-name="${redis.host}" 
    p:port="${redis.port}" 
    p:password="${redis.pass}"  
    p:pool-config-ref="poolConfig"/> 
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="poolConfig"/>
    <constructor-arg index="1" value="${redis.host}"/>
    <constructor-arg index="2" value="${redis.port}" type="int"/>
    <constructor-arg index="3" 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值