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

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

<!--jedis.jar -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> <!-- Spring下使用Redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>

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

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

@Configuration
@ComponentScan("the_mass.redis")
public class SpringConfig { //Spring连接工厂
@Bean
RedisConnectionFactory redisFactory () {
return new JedisConnectionFactory();
} //反序列化
@Bean
RedisTemplate redisTemplate () {
return new StringRedisTemplate(redisFactory());
}
}

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

第二部服务层

package the_mass.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; @Service
public class RedisService { @Autowired
RedisConnectionFactory factory; @Autowired
RedisOperations redisOperations; public void testRedis() {
RedisConnection connection = factory.getConnection();
byte[] bytes = connection.get("hello".getBytes());
System.out.println(new String(bytes, StandardCharsets.UTF_8));
} public void testRedisTemplate () {
Object hello = redisOperations.opsForValue().get("hello");
System.out.println(hello);
}
}

第三步

package the_mass.redis;

import redis.clients.jedis.Jedis;

public class JedisDemo {
public void execute () {
Jedis jedis = new Jedis();
//Jedis jedis1 = new Jedis("44.55.66.7", 3333); Boolean hello = jedis.exists("hello");
System.out.println(hello); String s = jedis.get("hello");
System.out.println(s); jedis.set("hello:1", "world:23"); Long hello1 = jedis.exists("hello", "hello:123");
System.out.println(hello1);
}
}

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

package the_mass.redis;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); RedisService redisService = context.getBean(RedisService.class);
//redisService.testRedis();
redisService.testRedisTemplate();
}
}

简单测试

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. Ubuntu系统---安装QQ

    使用Ubuntu很不方便,如果有什么消息的话,还要回到windows中查看.预想在Ubuntu上直接安装一个QQ,有网友说使用WebQQ发现老是掉线,于是这里安装QQ国际版. 首先,下载安装包. 这里 ...

  2. set调用add报错:

    Map<String, String> goodsStandMap = new HashMap<>();goodsStandMap.put("key1", ...

  3. Python&Selenium 数据驱动【unittest+ddt+json】

    一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后生成html测试报告 二.json文件 [ ...

  4. 17-SQLServer使用证书认证的方式搭建数据库镜像

    一.注意点 1.数据库的模式要是完整模式. 2.要对数据库完整备份和事务日志备份,分别还原到镜像库上,使用NORECOVERY模式. 3.镜像数据库是不允许删除和操作,即便查看属性也不行. 4.先删除 ...

  5. crypot.js 生成hash256, 在IE下的卡顿问题。

    项目需求: 上传大文件,调用crypto.js生成hash256码. 直接上传大文件,IE会直接崩溃. 于是利用file.slice分片检测.但是浏览器会出现卡顿问题.开始以为是内存泄漏.但看服务器进 ...

  6. MFC 线程启动、暂停、继续、终止

    CWinThread* p_myThread;//创建线程指针 BOOL flag_myThread = FALSE;//是否终止 //头文件中声明(放类内) static UINT MyThread ...

  7. SP4546 ANARC08A - Tobo or not Tobo IDA*

    题意:

  8. bbs--文章后台

    bbs--文章后台 需求分析 在个人文章后台中 1 有添加文章的接口 展示文章标题输入框 文本编辑框 标签 分类 选择框 提交 2 编辑文章的接口 页面与添加文章相同,就是把该文章的内容展示到文本编辑 ...

  9. tqdm如何在pandas里面使用

    原文: https://segmentfault.com/a/1190000016059726 当然,首先我们得载入模块,在notebook中使用tqdm带的基于Js显示的进度条前,请务必检查是否安装 ...

  10. P4779 【模板】单源最短路径(标准版)题解

    原题链接 https://www.luogu.org/problemnew/show/P4779 若还未食用弱化版的同学请先做这个qwq https://www.luogu.org/problemne ...