用的是最新的jedis-2.6.2.jar这个包,这个和以前的有点不同。还需要添加spring-data-redis-1.2.1.RELEASE.jar和commons-pool2-2.3.jar。

在类路径下创建spring-redis-config.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  10.  
  11. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  12. <property name="locations" value="classpath:redis.properties"/>
  13. </bean>
  14. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  15. <property name="maxIdle" value="300" />
  16. <property name="maxTotal" value="512" />
  17. <property name="maxWaitMillis" value="1000" />
  18. <property name="testOnBorrow" value="true" />
  19. </bean>
  20.  
  21. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  22. p:host-name="localhost" p:port="6379" p:password="" p:pool-config-ref="poolConfig"/>
  23.  
  24. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  25. <property name="connectionFactory" ref="connectionFactory" />
  26. </bean>
  27.  
  28. </beans>

由于引用配置文件,使用不了表达式,这里写死了。使用表达式启动就报错,我也不知道为什么。

  1. ##
  2. redis.host=localhost
  3. ##
  4. redis.port=6379
  5. ##
  6. redis.pass=
  7.  
  8. ##
  9. redis.maxIdle=300
  10. ##
  11. redis.maxTotal=512
  12. ##
  13. redis.maxWaitMillis=1000
  14. ##
  15. redis.testOnBorrow=true

redis.properties文件配置。

以前的版本应该有配置redis.maxActive但是看了源码,是没有setMaxActive方法的,所以不能注入,改用了redis.maxTotal。就因为这个弄了挺长时间的。

在web.xml配置spring-redis-config.xml文件

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>
  4. <!-- 多个配置用,隔开 -->
  5.  
  6. classpath*:spring-redis-config.xml
  7.  
  8. </param-value>
  9. </context-param>
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import org.springframework.data.redis.serializer.RedisSerializer;
  4.  
  5. public abstract class AbstractBaseRedisDao<V, K> {
  6.  
  7. @Autowired
  8. protected RedisTemplate<K, V> redisTemplate;
  9.  
  10. public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
  11. this.redisTemplate = redisTemplate;
  12. }
  13.  
  14. /**
  15. * 获取 RedisSerializer
  16. * <br>------------------------------<br>
  17. */
  18. protected RedisSerializer<String> getRedisSerializer() {
  19. return redisTemplate.getStringSerializer();
  20. }
  21. }

创建一个抽象类,然后让使用到的类都继承这个方法。

  1. @Service("areaRedisService")
  2. public class AreaRedisService<V, K> extends AbstractBaseRedisDao<V, K> {
  3.  
  4. public boolean add(final Area area) {
  5. boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
  6. public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
  7. RedisSerializer<String> serializer = getRedisSerializer();
  8. byte[] key = serializer.serialize(area.getId()+"");
  9. byte[] name = serializer.serialize(area.getName());
  10. return connection.setNX(key, name);
  11. }
  12. });
  13. return result;
  14. }
  15.  
  16. public Area get(final String keyId) {
  17. Area result = redisTemplate.execute(new RedisCallback<Area>() {
  18. public Area doInRedis(RedisConnection connection)
  19. throws DataAccessException {
  20. RedisSerializer<String> serializer = getRedisSerializer();
  21. byte[] key = serializer.serialize(keyId);
  22. byte[] value = connection.get(key);
  23. if (value == null) {
  24. return null;
  25. }
  26. String name = serializer.deserialize(value);
  27. return new Area(Integer.valueOf(keyId),name,null);
  28. }
  29. });
  30. return result;
  31. }
  32. }
  1. @Autowired
  2. AreaRedisService<?, ?> areaRedisService;
  3.  
  4. private String path = "/WEB-INF/jsp/";
  5.  
  6. @RequestMapping("/area/redis.htm")
  7. public ModelAndView areaRedis(HttpServletRequest request, HttpServletResponse response,String name) throws Exception {
  8. ModelAndView mv = new ModelAndView(path+"add.html");
  9. Area area = new Area();
  10. area.setCreateTime(new Date());
  11. area.setCommon(true);
  12. area.setDeleteStatus(false);
  13. area.setLevel(4);
  14. area.setName(name);
  15. area.setParentId(null);
  16. area.setSequence(1);
  17. area.setId(1);
  18. areaRedisService.add(area);
  19. mv.addObject("area", area);
  20. mv.addObject("arearedis",areaRedisService.get(area.getId()+""));
  21. return mv;
  22.  
  23. }

这是controller的方法,这里使用了spring的注解。

使用注解,需要在上面的spring-redis-config.xml文件加入<context:component-scan base-package="基础包路径"/>配置了扫描路径可以不配置<context:annotation-config/>,因为前面的包含了后面的,他会激活@Controller,@Service,@Autowired,@Resource,@Component等注解。

http://www.cnblogs.com/hjy9420/p/4278002.html

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语言学习】存储类型

    C语言中的存储类型主要有四种:auto.static.extern.register ★auto存储类型 默认的存储类型.在C语言中,假设忽略了变量的存储类型,那么编译器就会自己主动默认为auto型 ...

  2. 写一方法来实现两个变量的交换。在主调函数中定义两个整型变量,并初始化,调用交换方法,实现这两个变量的交换。(使用ref参数)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. hdu2629Identity Card

    Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...

  4. Ajax辅助方法

    目前为止,我们已经考察了如何编写客户端JavaScript代码,以发送并接受服务器的数据.然而,在使用ASP.NET MVC时,还有另一种方法可用来执行Ajax调用,这就是Ajax辅助方法. Ajax ...

  5. 设置QPushButton的平面与突出(遍历控件)

    #include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...

  6. 不直接访问远程的数据库,而是通过中间件(专业DBA的博客)

    建议不直接访问远程的数据库,而是通过中间件. 或者找到好的加密方式.http://blog.csdn.net/sqlserverdiscovery/article/details/8068318 在S ...

  7. 【HTTP 2】启用 HTTP 2(Starting HTTP/2)

    [HTTP 2]启用 HTTP 2(Starting HTTP/2) 四月 1, 2016 ~ LITECODES 前情提要 在上一篇文章<[HTTP 2]HTTP/2 协议概述(HTTP/2 ...

  8. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...

  9. C++部分术语(Terms)

    翻译自msdn,如有不妥当的地方,欢迎指正. 声明(Declaration):声明引入了一个名字以及其类型进入程序中,并没有定义一个相关的对象或者函数.然而,很多声明都作为定义使用.   定义(def ...

  10. 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)

    目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...