在Spring中使用Redis使用使用两个依赖包jedis.jar、spring-data-redis.jar

一下是Maven项目pom.xml添加依赖

  1. <!--jedis.jar -->
  2. <dependency>
  3. <groupId>redis.clients</groupId>
  4. <artifactId>jedis</artifactId>
  5. <version>2.9.0</version>
  6. </dependency>
  7.  
  8. <!-- Spring下使用Redis -->
  9. <dependency>
  10. <groupId>org.springframework.data</groupId>
  11. <artifactId>spring-data-redis</artifactId>
  12. <version>1.7.2.RELEASE</version>
  13. </dependency>

要注意的是jar包和Spring版本的兼容性问题

笔者这里是使用注解方式进行配置

  1. @Configuration
  2. @ComponentScan("the_mass.redis")
  3. public class SpringConfig {
  4.  
  5. //Spring连接工厂
  6. @Bean
  7. RedisConnectionFactory redisFactory () {
  8. return new JedisConnectionFactory();
  9. }
  10.  
  11. //反序列化
  12. @Bean
  13. RedisTemplate redisTemplate () {
  14. return new StringRedisTemplate(redisFactory());
  15. }
  16. }

在JedisConnectionFactory可以设置许多参数的在此使用的是本机默认就好了

第二部服务层

  1. package the_mass.redis;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.connection.RedisConnection;
  5. import org.springframework.data.redis.connection.RedisConnectionFactory;
  6. import org.springframework.data.redis.core.RedisOperations;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import java.nio.charset.StandardCharsets;
  10.  
  11. @Service
  12. public class RedisService {
  13.  
  14. @Autowired
  15. RedisConnectionFactory factory;
  16.  
  17. @Autowired
  18. RedisOperations redisOperations;
  19.  
  20. public void testRedis() {
  21. RedisConnection connection = factory.getConnection();
  22. byte[] bytes = connection.get("hello".getBytes());
  23. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  24. }
  25.  
  26. public void testRedisTemplate () {
  27. Object hello = redisOperations.opsForValue().get("hello");
  28. System.out.println(hello);
  29. }
  30. }

第三步

  1. package the_mass.redis;
  2.  
  3. import redis.clients.jedis.Jedis;
  4.  
  5. public class JedisDemo {
  6. public void execute () {
  7. Jedis jedis = new Jedis();
  8. //Jedis jedis1 = new Jedis("44.55.66.7", 3333);
  9.  
  10. Boolean hello = jedis.exists("hello");
  11. System.out.println(hello);
  12.  
  13. String s = jedis.get("hello");
  14. System.out.println(s);
  15.  
  16. jedis.set("hello:1", "world:23");
  17.  
  18. Long hello1 = jedis.exists("hello", "hello:123");
  19. System.out.println(hello1);
  20. }
  21. }

第四部在此笔者在redis里面已经有了一个Hello(key)

  1. package the_mass.redis;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
  8.  
  9. RedisService redisService = context.getBean(RedisService.class);
  10. //redisService.testRedis();
  11. redisService.testRedisTemplate();
  12. }
  13. }

简单测试

Spring下使用Redis的更多相关文章

  1. java配置SSM框架下的redis缓存

    pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...

  2. Spring下redis的配置

    这个项目用到redis,所以学了一下怎样在Spring框架下配置redis. 1.首先是在web.xml中添加Spring的配置文件. <web-app version="3.0&qu ...

  3. Java Spring mvc 操作 Redis 及 Redis 集群

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...

  4. spring中订阅redis键值过期消息通知

    1.首先启用redis通知功能(ubuntu下操作):编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知): notify-keyspace-events Ex 或者登陆 ...

  5. Spring Data操作Redis详解

    Spring Data操作Redis详解 Redis是一种NOSQL数据库,Key-Value形式对数据进行存储,其中数据可以以内存形式存在,也可以持久化到文件系统.Spring data对Redis ...

  6. 【Spring】17、spring cache 与redis缓存整合

    spring cache,基本能够满足一般应用对缓存的需求,但现实总是很复杂,当你的用户量上去或者性能跟不上,总需要进行扩展,这个时候你或许对其提供的内存缓存不满意了,因为其不支持高可用性,也不具备持 ...

  7. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  8. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  9. (转)spring boot整合redis

    一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...

随机推荐

  1. 07_Redis_Sorted Set命令

    一:Redis 有序集合(sorted set):有序set集合,专门用来做排行榜 Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员 ------- (有序不重复) 不 ...

  2. jsp下拉列表

    <c:set var="REPORT_TYPE_NORMAL" value="<%=SysIndexFormTemp.REPORT_TYPE_NORMAL%& ...

  3. a 标签的download 属性在谷歌浏览器下无法下载图片,如何处理?

    a 标签的download属性在下载图片文件的时候是如何的方便,可是可是谷歌浏览器不支持下载,而是下载打开新窗口预览图片.这个兼容性问题如何解决呢? 了解了一番,HTMLCanvasElement.t ...

  4. Vue完成页面切换中加载数据

    created() { // 拿到路由传递来的car主键 let pk = this.$route.query.pk || this.$route.params.pk; // 主键不存在,就直接结束方 ...

  5. 在laravel5.8中集成swoole组件----用协程实现的服务端和客户端(二)---静态文件如何部署

    目前,较为成熟的技术是采用laravelS组件,注意和laravel 区别laravelS多了一个大写的S,由于laravelS默认监听5200端口,所以laravel项目要做一些调整 例如: 静态文 ...

  6. 使用 stringstream 进行类型转换

    如何用使用stringstream进行类型转换: 1. 下面例子为整型和sting类型的相互转换示例 整型转换为字符串类型 string NumberToString(int num){    str ...

  7. linux如何查看ip地址

    使用命令: ifconfig -a 例如:

  8. Depth from Videos in the Wild 解读

    2019年7月17日11:37:05 论文 Depth from Videos in the Wild: Unsupervised Monocular Depth Learning from Unkn ...

  9. Bert系列 源码解读 四 篇章

    Bert系列(一)——demo运行 Bert系列(二)——模型主体源码解读 Bert系列(三)——源码解读之Pre-trainBert系列(四)——源码解读之Fine-tune 转载自: https: ...

  10. lua.c:82:10: fatal error: readline/readline.h: No such file or directory #include <readline/readline.h>

    make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all ...