Spring整合Redis
1.相关jar包 除了Spring必须的jar外,还需要spring-data-redis,jedis,commons-pool,这里使用的是maven,也可以拿着url把jar包下下来
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6..RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
2.Spring的配置文件 root-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value=""></property>
<property name="maxIdle" value=""></property>
<property name="testOnBorrow" value="true"></property>
<property name="maxWaitMillis" value=""></property>
</bean>
<!-- 配置连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.23.128"></property>
<property name="port" value=""></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- 配置模板 -->
<!-- 注意:StringRedisTemplate是RedisTemplate的子类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean> </beans>
3.测试
package com.spring.wzy; import java.io.File; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; public class MySpringJedis {
public static ApplicationContext app;
static{ String se = File.separator;
app = new FileSystemXmlApplicationContext("E:"+se+"workspace"+se+"SpringSource"+se+"src"+se+"main"+se+"webapp"+se+"WEB-INF"+se+"spring"+se+"root-context.xml"); } public static void main(String[] args) {
/**
* 当配置好JedisConnectionFactory后,就可以使用redis了
* 但是jedisFactory.getConnection()提供的API比较原生态,
* 所以又封装了一层RedisTemplate,RedisTemplate提供的API是比较好用的,
* 其实但本质一样
* */ // JedisConnectionFactory jedisFactory = (JedisConnectionFactory)app.getBean("jedisConnectionFactory");
// jedisFactory.getConnection().set("k2".getBytes(), "testk2".getBytes());
// System.out.println(new String(jedisFactory.getConnection().get("str01".getBytes()))); /**
* 以下5个接口提供了redis的5种数据类型的API
* opsForValue()对应redis的String
* opsForList()对应redis的List
* opsForHash()对应redis的Hash
* opsForSet()对应redis的Set
* opsForZSet()对应redis的ZSet
* */ RedisTemplate redisTemplate = (RedisTemplate)MySpringJedis.app.getBean("redisTemplate");
System.out.println(redisTemplate);
// redisTemplate.slaveOf(host, port);//主从复制
// redisTemplate.slaveOfNoOne();
// redisTemplate.delete(key);
// redisTemplate.multi();//开启事务
// redisTemplate.exec()//执行事务
// redisTemplate.opsForList().leftPop(key);
// redisTemplate.opsForHash().get(key, hashKey);
// redisTemplate.opsForSet().add(key, values);
// redisTemplate.opsForValue().set(key, value);;
// redisTemplate.opsForZSet().add(key, tuples);
/**
* 绑定某个key,之后的操作都是对这个key的操作
* */
//BoundHashOperations<H, HK, HV> b = redisTemplate.boundHashOps(key)
//BoundSetOperations<K, V> b = redisTemplate.boundSetOps(key)
//BoundListOperations<String, Object> b = redisTemplate.boundListOps("arr");
//BoundValueOperations<String, String> b = redisTemplate.boundValueOps("arr");
//BoundZSetOperations<K, V> b = redisTemplate.boundZSetOps(key)
}
}
Spring整合Redis的更多相关文章
- 网站性能优化小结和spring整合redis
现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录 ...
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- spring整合redis之hello
1.pom.xml文件 <dependencies> <!-- spring核心包 --> <dependency> <groupId>org.spri ...
- Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object
我在Spring整合Redis时报错,我是犯了一个很低级的错误! 我设置了Redis的访问密码,在Spring的配置文件却没有配置密码这一项,配置上密码后,终于不报错了!
- Redis的安装以及spring整合Redis时出现Could not get a resource from the pool
Redis的下载与安装 在Linux上使用wget http://download.redis.io/releases/redis-5.0.0.tar.gz下载源码到指定位置 解压:tar -xvf ...
- Spring整合redis实现key过期事件监听
打开redis服务的配置文件 添加notify-keyspace-events Ex 如果是注释了,就取消注释 这个是在以下基础上进行添加的 Spring整合redis:https://www. ...
- (转)Spring整合Redis作为缓存
采用Redis作为Web系统的缓存.用Spring的Cache整合Redis. 一.关于redis的相关xml文件的写法 <?xml version="1.0" ...
- spring整合redis使用RedisTemplate的坑Could not get a resource from the pool
一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...
- Spring整合redis,通过sentinel进行主从切换
实现功能描述: redis服务器进行Master-slaver-slaver-....主从配置,通过2台sentinel进行failOver故障转移,自动切换,采用该代码完全可以直接用于实际生产环境. ...
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
随机推荐
- SQL Server帐号孤立的问题解决
网站近日经常遭到攻击,好几次数据库挂马,前几天把论坛升级了,今天又升级了数据库,把之前的MSSQL 2000 升级到MSSQL 2005,用的是数据库还原功能还原的,遇到了这个帐号孤立的问题. 什么是 ...
- Java字节、十进制、十六进制、字符串之间的相互转换
1. 字节转10进制 直接使用(int)类型转换. /* * 字节转10进制 */ public static int byte2Int(byte b){ int r = (int) b; retur ...
- Tomcat 8080端口被占用解决方法
使用lsof命令查看端口占用情况 sudo lsof -i:8080 端口占用情况 java 1564 tomcat8 50u IPv6 19336 0t0 TCP *:http-alt (LISTE ...
- jQuery组件开发之表格隔行选中效果实现
一.效果展示如下 jQuery组件之表格插件源码 //表格选中插件 //方式一 (function($){ var chosTabBgColor = function(options){ //设置默认 ...
- kail linux 虚拟机安装实录(一) 新建虚拟机
各位晚上好. 现在开始进行kail linux 在虚拟机上的安装. 我所使用的工具如下: kail linux 2.0 x64 http://mirrors.neusoft.edu.cn/kal ...
- SharePoint 2013 搭建app本地开发环境
使用SharePoint App,如果要通过应用程序目录分发 SharePoint 相关应用程序,如具有完全控制权限的 SharePoint 相关应用程序(无法部署到 Office 365 网站),则 ...
- iOS应用中的相关正则及验证
1.手机号码的验证正则 正则表达式: ^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$ 详细解释 解释: ^...$: ^:开始 $:结束 中间为要处理的字串 ...
- IOS开发基础知识--碎片32
1:动画属性UIViewAnimationOptions说明 a:常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子 ...
- OOP感悟
行内讲的最多的就是出来一年内基本靠copy,一年后才基本懂得如何去写代码,而理解领悟oop,需要的时间却不定. 我工作中,我发现很多人拿着面向对相当的语言作者做着面向过程的事情,不需要知其所以然,只要 ...
- iOS之深拷贝与浅拷贝
在最开始,我们需要清楚一些关于内存分配方式的基础知识. 一般来说分为栈.堆.静态变量存储区.全局变量存储区.代码区. 前两个大家都懂的.通常将后三个合并称之为静态存储区,存储的是一些全局变量.静态变量 ...