redis与spring整合实例

1)首先是redis的配置。

使用的是maven工程,引入redis与spring整合的相关jar包

<!-- redis服务 start-->
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.6.2.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.6.2</version>
        </dependency>
        <!-- redis服务 end-->

2)在redis中增加相应配置文件: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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    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">  
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig" /> 
         <property name="port" value="${redis.port}" /> 
         <property name="hostName" value="${redis.host}" /> 
         <property name="password" value="${redis.pass}" /> 
        </bean>
    <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>    
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>    
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="stringSerializer" />
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="stringSerializer" />
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
    </bean>
</beans>

3)在application中配置redis连接池需要的相关参数

#redis连接配置===================start=========================
# Redis settings
redis.host=192.168.10.102
redis.port=6379
redis.pass=
redis.maxIdle=1
redis.maxActive=9
redis.maxWait=1000
redis.testOnBorrow=true
#redis连接配置===================end=========================

4)在web.xml中引入相应的配置信息

 <!-- ContextLoaderListener初始化Spring上下文时需要使用到的contextConfigLocation参数 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 配置spring.xml和spring-mybatis.xml这两个配置文件的位置,固定写法 -->
        <param-value>
            classpath:spring.xml,
            classpath:spring-mybatis.xml,
            classpath:spring-activitymq.xml,
            classpath:dubbo.xml,
            classpath:spring-redis.xml
        </param-value>
    </context-param>

5)启动tomcat服务,启动正常。

原文地址:https://www.cnblogs.com/a393060727/p/5281929.html