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的更多相关文章

  1. 网站性能优化小结和spring整合redis

    现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录 ...

  2. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  3. spring整合redis之hello

    1.pom.xml文件 <dependencies> <!-- spring核心包 --> <dependency> <groupId>org.spri ...

  4. Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object

    我在Spring整合Redis时报错,我是犯了一个很低级的错误! 我设置了Redis的访问密码,在Spring的配置文件却没有配置密码这一项,配置上密码后,终于不报错了!

  5. 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 ...

  6. Spring整合redis实现key过期事件监听

    打开redis服务的配置文件   添加notify-keyspace-events Ex  如果是注释了,就取消注释 这个是在以下基础上进行添加的 Spring整合redis:https://www. ...

  7. (转)Spring整合Redis作为缓存

           采用Redis作为Web系统的缓存.用Spring的Cache整合Redis. 一.关于redis的相关xml文件的写法 <?xml version="1.0" ...

  8. spring整合redis使用RedisTemplate的坑Could not get a resource from the pool

    一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...

  9. Spring整合redis,通过sentinel进行主从切换

    实现功能描述: redis服务器进行Master-slaver-slaver-....主从配置,通过2台sentinel进行failOver故障转移,自动切换,采用该代码完全可以直接用于实际生产环境. ...

  10. SpringBoot开发二十-Redis入门以及Spring整合Redis

    安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...

随机推荐

  1. 用C#读取相片(JPG图片)的EXIF信息的方法

    引言:EXIF,是英文Exchangeable Image File{}#endregion#region 数据转换结构/// summary>/// 转换数据结构/// /summary> ...

  2. [moka同学笔记]八、Yii2.0课程笔记(魏曦老师教程)[授权]

    数据库表创建 在执行yii rbac/init之前,需要在 \console\controllers\RbacController.php中 <?php namespace console\co ...

  3. jQuery DateTimePicker 日期控件

    在线实例 实例演示 使用方法 <input id="datetimepicker" type="text" > 复制 $('#datetimepic ...

  4. HTML5树叶飘落动画

    查看效果:http://keleyi.com/keleyi/phtml/css3/15.htm 请使用Chrome浏览器查看本效果. html源代码: <!DOCTYPE HTML> &l ...

  5. webpack继续

    序言:继续上一篇<webpack初入> 1.上一篇配置完成后最终的命令是:webpack,如果更改package.json中的一个配置如下: 换为 此时最终的命令:npm start等同于 ...

  6. SharePoint 2013 状态机工作流之UpdateItemActivity

    没什么可说的,一个Activity的使用介绍,其他类似的Activity也可以参考这个使用. 1.添加ApplyActivation和UpdateItemActivity,在onWorkflowAct ...

  7. 记jQuery.fn.show的一次踩坑和问题排查

    最近很少已经很少用jQuery,因为主攻移动端,常用Zepto,其实很多细节和jQuery并不一样.最近又无意中接触到了PC的需求和IE6, 使用了jQuery,刚好踩坑了,特意记录一下. 本文内容如 ...

  8. iOS用三种途径实现一方法有多个返回值

    以前觉得这种标题有点偏向于理论,实际开发中怎么会有这种诡异的需求,但是真正遇到了这种硬需求时觉得还是有那么点价值的,理论付诸了实践在此也就做了个整理. 以我私下开发中的一处代码为例,本意是希望有这么一 ...

  9. Java源码分析之LinkedList

    LinkedList与ArrayList正好相对,同样是List的实现类,都有增删改查等方法,但是实现方法跟后者有很大的区别. 先归纳一下LinkedList包含的API 1.构造函数: ①Linke ...

  10. ORA-00824: cannot set sga_target due to existing internal settings, see alert log for more information

    这篇文章是上篇文章”Expdp 导数错误 ORA-00832”的延续,前几天工作比较忙.累,直到今天才整理发出来.这个数据库实例的参数设置比较诡异其实是有原因的,由于这台数据库服务器系统是32位,数据 ...