JAVA记录-SpringMVC集成redis

1.redis.properties

#主机地址
redis.host=127.0.0.1
#端口号
redis.port=6379
#当池内没有返回对象时,最大等待时间毫秒数 
redis.pool.maxWaitMillis=10000
#最大连接数
redis.pool.maxTotal=300
#最大能够保持idel空闲状态的对象数  
redis.pool.maxIdle=20
#最小能够保持idel空闲状态的对象数  
redis.pool.minIdle=5
#当调用borrow Object方法时,是否进行有效性检查   
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查   
redis.pool.testOnReturn=true
#超时
redis.timeout=15000
#是否用池
redis.usePool=true
#“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.  
redis.pool.timeBetweenEvictionRunsMillis=30000
#向调用者输出“链接”对象时,是否检测它的空闲超时;  
redis.pool.testWhileIdle=true  
# 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.  
redis.pool.numTestsPerEvictionRun=50 

2.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com.net.xinfang" />
    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>  
     <!-- redis连接池的配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
      <property name="maxTotal" value="${redis.pool.maxTotal}" />
      <property name="maxIdle" value="${redis.pool.maxIdle}" />
      <property name="minIdle" value="${redis.pool.minIdle}" />
      <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
      <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
      <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
    </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="poolConfig" ref="jedisPoolConfig" />
        <property name="timeout" value="${redis.timeout}"></property> 
        <property name="usePool" value="${redis.usePool}"></property> 
    </bean>
    <bean id="jedisTemplate" 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>
原文地址:https://www.cnblogs.com/xinfang520/p/7700023.html