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如何设置key的有效期

    redis如何设置key的有效期

    这篇文章主要介绍了redis如何设置key的有效期方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 使用Redis实现延时任务的解决方案

    使用Redis实现延时任务的解决方案

    这篇文章主要介绍了使用Redis实现延时任务的解决方案,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • redis中List列表常见命令及使用场景

    redis中List列表常见命令及使用场景

    这篇文章主要给大家介绍了关于redis中List列表常见命令及使用场景的相关资料,Redis列表是简单的字符串列表,按照插入顺序排序,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • Redis list 类型学习笔记与总结

    Redis list 类型学习笔记与总结

    这篇文章主要介绍了Redis list 类型学习笔记与总结,本文着重讲解了关于List的一些常用方法,比如lpush 方法、lrange 方法、rpush 方法、linsert 方法、 lset 方法等,需要的朋友可以参考下
    2015-06-06
  • redis中事务机制及乐观锁的实现

    redis中事务机制及乐观锁的实现

    这篇文章主要介绍了redis中事务机制及乐观锁的相关内容,通过事务的执行分析Redis乐观锁,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • Redis sentinel节点如何修改密码

    Redis sentinel节点如何修改密码

    这篇文章主要介绍了Redis sentinel节点如何修改密码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • CentOS 6.6下Redis安装配置记录

    CentOS 6.6下Redis安装配置记录

    这篇文章主要介绍了CentOS 6.6下Redis安装配置记录,本文给出了安装需要的支持环境、安装redis、测试Redis、配置redis等步骤,需要的朋友可以参考下
    2015-03-03
  • redis加锁的三种方式小结

    redis加锁的三种方式小结

    本文主要介绍了redis加锁的三种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Redis禁用命令、危险命令及规避方法

    Redis禁用命令、危险命令及规避方法

    这篇文章主要介绍了Redis禁用命令、危险命令及规避方法,本文介绍了个非常致命的两个命令以及用配置文件禁用这些命令的方法,需要的朋友可以参考下
    2015-06-06
  • 利用控制台如何对Redis执行增删改查命令

    利用控制台如何对Redis执行增删改查命令

    这篇文章主要给大家介绍了关于利用控制台如何对Redis执行增删改查命令的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08

最新评论