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配置内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springmvc配置线程池Executor做多线程并发操作的代码实例

    加载xml文件 在ApplicationContext.xml文件里面添加 xmlns:task="http://www.springframework.org/schema/task" xmlns文件并且xsi:schemaLocation中添加 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 在spring中配置Executor

  • spring mvc 读取xml文件数据库配置参数的方法

    本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到xml文件里面去,方便部署. spring mvc 4.2.6项目 SQL Server 2008数据库 本文介绍的主要使用ApplicationContext以及其实现类实现.主要用到的是ClassPathXmlApplicationContext. ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载 完成ApplicationContext

  • SpringMVC+ZTree实现树形菜单权限配置的方法

    计划在开源项目里加入权限配置的功能,打算加入zTree实现树形结构. Team的Github开源项目链接:https://github.com/u014427391/jeeplatform欢迎star(收藏) zTree 是一个依靠 jQuery 实现的多功能 "树插件".优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree下载链接:http://www.treejs.cn/v3/main.php#_zTreeInfo 角色信息实体类: package org.

  • Spring Boot配置接口WebMvcConfigurer的实现

    WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制.基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapt

  • Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法

    前言 本文主要介绍的是关于Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法,分享出来供大家参考学习,下面来看看详细的介绍: 实现方法: 数据源在配置文件中的配置 <pre name="code" class="java"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spring

  • springmvc整合freemarker配置的详细步骤

    一.对应的导包(有些包是不必须的) <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  • 详解IDEA用maven创建springMVC项目和配置

    本文介绍了IDEA用maven创建springMVC项目和配置,分享给大家,具体如下: 工具准备:IDEA2016.3 Java jdk 1.8 1.DEA创建项目 新建一个maven project,并且选择webapp原型. 然后点击next 这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样然后next 为了快一点创建,我们添加一个属性值,如图中亮的所示,点右边的加号,name=archetypeCatalog value=internal.

  • 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&

  • SpringBoot集成Redis实现消息队列的方法

    list 原理说明 Redis 的 list 是按照插入顺序排序的字符串链表. 如图所示,可以通过 lpush 和 rpop 或者 rpush 和 lpop 实现消息队列. 1 lpush 和 rpop 2 rpush 和 lpop 消息队列功能实现 引入 Redis 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data

  • SpringBoot+redis配置及测试的方法

    1.创建项目时选择redis依赖 2.修改配置文件,使用SpringBoot就避免了之前很多的xml文件 2.1学过redis的同学都知道这个东西有集群版也有单机版,无论哪个版本配置起来都很简单 2.1.1首先找到配置文件 2.1.2然后配置集群版,直接在配置文件内编辑即可 2.1.3配置单机版 3.测试 找到测试文件夹,自动注入redis模板 4.分别测试操作String和Hash类型的数据 4.1操作String @Test public void testString(){ //操作Str

  • SpringBoot集成redis错误问题及解决方法

    目录 一.错误信息 二.软件版本 三.基本信息 五.其他信息 描述:SpingBoot 集成Reids (本机连接虚拟机Redis服务)出现错误 哇咔咔 当你距离成功只有一步的时候 论那个时候的心情 七上八下!!!! 一.错误信息 org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is org.springframework.da

  • redis配置认证密码的方法

    1.通过配置文件进行配置 yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 #requirepass foobared 去掉行前的注释,并修改密码为所需的密码,保存文件 requirepass myRedis 重启redis sudo service redis restart #或者 sudo service redis stop sudo redis-server /etc/redis.conf 这个时候尝试登录redis,发现可以登上,但是执行具体

  • 使用SpringBoot集成redis的方法

    今天,日月在这里教大家如何使用springBoot集成redis,说实话比较简单,网上也有大把的教程.先套用一下网上的简介. 定义 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 它通常被称为数据结构服务器,因为值(value)可以是 字符串(S

  • Spring Boot 快速集成 Redis的方法

    Spring Boot 如何快速集成 Redis?没错,栈长本文教你,让大家少走弯路! 添加依赖 使用像 Redis 这类的 NoSQL 数据库就必须要依赖 spring-data-redis 这样的能力包,开箱即用,Spring Boot 中都封装好了: 引入spring-boot-starter-data-redis: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>

  • springmvc集成使用redis过程

    目录 Redis安装 spring集成redis SpringMVC中使用redis 写进和读取redis Redis安装 首先安装redis.这个就不重点介绍了.windos下载redis就行. 我用的是mac 用命令行安装的. 安装命令 yum install redis 运行命令 sudo redis-server 这样就安装运行成功了. spring集成redis 首先你需要下载驱动包,下载 jedis.jar,确保下载最新驱动包.然后导包. 在spring配置文件里我这是Applica

  • 详解SpringMVC拦截器配置及使用方法

    本文介绍了SpringMVC拦截器配置及使用方法,分享给大家,具体如下: 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面: 3.性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录): 4.通用行为:读取cookie得到用户信

  • vue项目中运用webpack动态配置打包多种环境域名的方法

    在如今前后端分离,各种框架盛行的前端界,对项目的打包要求也越来越复杂,本人分享一个vue项目里,根据命令行输入不同的命令,打包出不同环境域名的方法.(欢迎纠错,谢谢.) 1. 安装插件 cross-env,npm install cross-env --save -dev,用于配置命令行输入命令. 2. 修改package.json里的script命令: 配置了test(测试),ready(预发布),prod(正式)三种环境,npm run build 默认设置成 npm run build:p

随机推荐