iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >SpringMVC集成redis配置的多种实现方法
  • 148
分享到

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

2024-04-02 19:04:59 148人浏览 独家记忆
摘要

第一步:下载并安装Redis(网上已经有很多安装教程在此不细讲了) 第二步:pom文件引入jar包 在此需要注意Redis和jedis连接工厂版本 redsi:https://mvn

第一步:下载并安装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配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringMVC集成redis配置的多种实现方法

本文链接: https://www.lsjlt.com/news/122564.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringMVC集成redis配置的多种实现方法
    第一步:下载并安装Redis(网上已经有很多安装教程在此不细讲了) 第二步:pom文件引入jar包 在此需要注意Redis和jedis连接工厂版本 redsi:https://mvn...
    99+
    2024-04-02
  • Redis集群的三种配置方式案例
    前言 Redis有三种集群模式: 主从复制哨兵(Sentinel)集群(Cluster) 💕主从复制 读写分离 在多个redis实例建立起主从关系,当主redis 中的数据发生变化,从redis 中的数据也会同步变化。 通...
    99+
    2023-09-09
    redis 数据库 java
  • Springboot集成Jasypt实现配置文件加密的方法
    目录Jasypt介绍Jasypt好处应用场景使用方式实战使用Windows环境变量方式指定Linux环境变量方式进行指定Jasypt介绍 Jasypt是一个java库,它允许开发员以...
    99+
    2023-05-18
    Springboot集成Jasypt Springboot集成Jasypt文件加密
  • Java实现数据集合的多种方法
    ////=================显示连接型列表的使用========= import java.util.*; public class lis...
    99+
    2023-06-03
  • Redis操作多个数据库的配置的方法实现
    目录前言一、添加pom 依赖二、多数据源的配置和添加到spring容器中三、使用方式结语:前言 redis 默认有 0-16 号数据库,一般我们操作redis时,用的是 0号数据库,...
    99+
    2024-04-02
  • redis集群配置的方法是什么
    在Redis中配置集群需要进行以下步骤: 启动集群模式:首先需要修改Redis的配置文件,将cluster-enabled设置为...
    99+
    2024-04-02
  • 详解Spring集成Redis的两种方式
    目录一、使用Jedis方式集成1、增加依赖2、配置项3、配置连接池4、测试使用spring-data-redis1、引入依赖2、配置项3、使用4、可能会遇到的坑哨兵和集群总结:在工作...
    99+
    2024-04-02
  • springboot redis集群配置的方法是什么
    要配置Spring Boot中的Redis集群,可以使用以下方法:1. 添加Redis依赖项:在`pom.xml`文件中添加Spri...
    99+
    2023-09-16
    springboot redis
  • Springboot怎么集成mybatis实现多数据源配置
    本文小编为大家详细介绍“Springboot怎么集成mybatis实现多数据源配置”,内容详细,步骤清晰,细节处理妥当,希望这篇“Springboot怎么集成mybatis实现多数据源配置”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入...
    99+
    2023-07-02
  • Spring集成MongoDB的两种方法实例
    目录前言一、准备工作1、工程生成2、配置项二、使用MongoTemplate1、创建实体UserInfo2、定义接口3、接口实现4、定义调用controller5、结果三、使用Mon...
    99+
    2024-04-02
  • SpringBoot集成slf4j日志配置的方法
    目录前言 1、slf4j概述 2、pom.xml的日志依赖 3、application.yml的日志配置 4、logback.xml配置文件定义 5、logback.xml配置文件解...
    99+
    2024-04-02
  • Spring通过Java配置集成Tomcat的方法
    添加Tomcat依赖 <!-- 自己编译的版本--> <dependency> <groupId>org.apache</gro...
    99+
    2024-04-02
  • 详解SpringMVC的两种实现方式
    目录一、方法一:实现Controller接口二、方法二:使用注解开发一、方法一:实现Controller接口 这个在我的第一个SpringMVC程序中已经学习过了,在此不作赘述,现在...
    99+
    2022-11-13
    SpringMVC实现方式 SpringMVC的两种实现方式
  • Springboot集成mybatis实现多数据源配置详解流程
    新建springboot工程,引入web、mysql、mybatis依赖 <dependency> <groupId>org.sp...
    99+
    2024-04-02
  • Redis集群高可用配置的方法是什么
    Redis集群高可用配置的方法有以下几种: 主从复制:通过配置Redis实例的主从关系,主节点负责写操作,从节点负责读操作和主节...
    99+
    2023-10-24
    Redis
  • SpringBoot集成短信和邮件的配置方法
    本文小编为大家详细介绍“SpringBoot集成短信和邮件的配置方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot集成短信和邮件的配置方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。准备工...
    99+
    2023-06-30
  • Centos7下Redis主从搭建配置的实现方法
    这篇文章将为大家详细讲解有关Centos7下Redis主从搭建配置的实现方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、环境介绍Redis—master ...
    99+
    2024-04-02
  • springboot配置多个数据源两种方式实现
    目录第一种方式:方法二在我们的实际业务中可能会遇到;在一个项目里面读取多个数据库的数据来进行展示,spring对同时配置多个数据源是支持的。 本文中将展示两种方法来实现这个功能。 s...
    99+
    2024-04-02
  • Spring Boot 集成 Kafkad的实现方法
    本篇内容介绍了“Spring Boot 集成 Kafkad的实现方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Spring Boot 作...
    99+
    2023-06-14
  • easycode配置成mybatis-plus模板的实现方法
    本文主要介绍了easycode配置成mybatis-plus模板的实现方法,分享给大家,具体如下: entity.java ##导入宏定义 $!define ##保存文件(宏定义...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作