SpringMVC集成redis配置的多种实现方法

 更新时间:2021年03月29日 14:58:31   作者:Abdulaziz_Dev  
这篇文章主要介绍了SpringMVC集成redis配置的多种实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

第一步:下载并安装Redis(网上已经有很多安装教程在此不细讲了)

第二步:pom文件引入jar包
在此需要注意Redis和jedis连接工厂版本
redsi:https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
jedis:https://mvnrepository.com/artifact/redis.clients/jedis

 <!-- redis -->
		<dependency> 
		 <groupId>org.springframework.data</groupId> 
		 <artifactId>spring-data-redis</artifactId> 
		 <version>1.7.2.RELEASE</version> 
		</dependency> 
		<dependency> 
		 <groupId>redis.clients</groupId> 
		 <artifactId>jedis</artifactId> 
		 <version>2.9.0</version> 
		</dependency>

第三步:配置redis.properties文件

# Redis Setting
# Redis默认有16个库,序号是0-15,默认是选中的是0号数据库
spring.redis.database=0 
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口,默认是6379
spring.redis.port=6379 
# Redis服务器连接密码(默认为空)
# spring.redis.password=你的密码
# 连接池最大阻塞等待时间(使用负值表示没有限制),根据实际情况修改
spring.redis.pool.maxWaitMillis=-1 
# 连接池中的最大空闲连接,根据实际情况修改
spring.redis.pool.maxIdle=8 
# 连接池中的最小空闲连接,根据实际情况修改
spring.redis.pool.minIdle=0 
# 连接超时时间(毫秒),根据实际情况修改
spring.redis.timeout=2000 

第四步:配置spring-redis-config.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:cache="http://www.springframework.org/schema/cache"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:redis="http://www.springframework.org/schema/redis" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
  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-4.0.xsd
  http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
 
 <!-- 载入redis.properties,这里要特别注意,如果有多个properties文件,必须用逗号分开,不能写成两个 <context:property-placeholder/> -->
 <context:property-placeholder location="classpath:redis.properties" />
 
 <!-- 配置JedisPoolConfig连接池-->
 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  <property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
  <property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
  <property name="maxWaitMillis" value="${spring.redis.pool.maxWaitMillis}"></property>
 </bean>
 
 <!-- 配置jedis连接工厂 -->
 <bean id="connectionFactory"
   class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <property name="poolConfig" ref="poolConfig"></property>
  <property name="hostName" value="${spring.redis.host}"></property>
  <property name="port" value="${spring.redis.port}"></property>
<!--   <property name="password" value="${spring.redis.password}"></property> -->
  <property name="database" value="${spring.redis.database}"></property>
  <property name="timeout" value="${spring.redis.timeout}"></property>
 </bean>
 
 <!-- 配置RedisTemplate -->
 <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
 <bean id="cacheRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="keySerializer" ref="stringRedisSerializer" />
  <property name="hashKeySerializer" ref="stringRedisSerializer" />
  <property name="valueSerializer" ref="stringRedisSerializer" />
  <property name="hashValueSerializer" ref="stringRedisSerializer" />
 </bean>
</beans>

第五步:spring集成spring-redis文件
方式一:在spring配置文件中加入:

<import resource="classpath:spring-redis-config.xml"/>

方式二:直接将spring-redis-config的东西写到spring配置文件里。

spring集成Redis基本配置完成!

到此这篇关于SpringMVC集成redis配置的多种实现方法的文章就介绍到这了,更多相关SpringMVC集成redis配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • redis保存AtomicInteger对象踩坑及解决

    redis保存AtomicInteger对象踩坑及解决

    这篇文章主要介绍了redis保存AtomicInteger对象踩坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 利用Redis的有序集合实现排行榜功能实例代码

    利用Redis的有序集合实现排行榜功能实例代码

    这篇文章主要给大家介绍了关于如何利用Redis的有序集合实现排行榜功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Redis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Redis异步队列的实现及应用场景

    Redis异步队列的实现及应用场景

    异步队列是一种底层基于异步 I/O 模型的消息队列,用于在分布式系统中进行同步和异步的通讯和协作,本文主要介绍了Redis异步队列的实现及应用场景,感兴趣的可以了解一下
    2023-12-12
  • Redis高并发缓存问题分析及解决过程

    Redis高并发缓存问题分析及解决过程

    文章总结了Redis缓存的六种常见问题及其解决方案:缓存穿透、缓存击穿、缓存雪崩、热点key重建优化、缓存和数据库双写不一致,以及Redis对过期key的三种清除策略,每种问题都提供了详细的原因分析和具体的解决方案
    2025-01-01
  • redis缓存与数据库一致性的问题及解决

    redis缓存与数据库一致性的问题及解决

    这篇文章主要介绍了redis缓存与数据库一致性的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • redis复制有可能碰到的问题汇总

    redis复制有可能碰到的问题汇总

    这篇文章主要介绍了redis复制有可能碰到的问题汇总,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Redis源码解析:集群手动故障转移、从节点迁移详解

    Redis源码解析:集群手动故障转移、从节点迁移详解

    这篇文章主要介绍了Redis源码解析:集群手动故障转移、从节点迁移的相关内容,涉及通过集群定时器函数clusterCron实现从节点迁移等知识,具有一定参考价值,需要的朋友可以了解。
    2017-10-10
  • Ubuntu系统中Redis的安装步骤及服务配置详解

    Ubuntu系统中Redis的安装步骤及服务配置详解

    本文主要记录了Ubuntu服务器中Redis服务的安装使用,包括apt安装和解压缩编译安装两种方式,并对安装过程中可能出现的问题、解决方案进行说明,以及在手动安装时,服务器如何添加自定义服务的问题,需要的朋友可以参考下
    2024-12-12
  • Redis多种内存淘汰策略及配置技巧分享

    Redis多种内存淘汰策略及配置技巧分享

    本文介绍了 Redis 内存满时的淘汰机制,包括内存淘汰机制的概念,Redis 提供的 8 种淘汰策略(如 noeviction、volatile-lru 等)及其适用场景,还讲解了如何配置淘汰机制,通过合理配置可提高缓存效率和系统性能,需要的朋友可以参考下
    2025-01-01
  • 一文详解如何停止/重启/启动Redis服务

    一文详解如何停止/重启/启动Redis服务

    Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统,这篇文章主要给大家介绍了关于如何停止/重启/启动Redis服务的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03

最新评论