Spring下使用Redis
在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的更多相关文章
- java配置SSM框架下的redis缓存
pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...
- Spring下redis的配置
这个项目用到redis,所以学了一下怎样在Spring框架下配置redis. 1.首先是在web.xml中添加Spring的配置文件. <web-app version="3.0&qu ...
- Java Spring mvc 操作 Redis 及 Redis 集群
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...
- spring中订阅redis键值过期消息通知
1.首先启用redis通知功能(ubuntu下操作):编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知): notify-keyspace-events Ex 或者登陆 ...
- Spring Data操作Redis详解
Spring Data操作Redis详解 Redis是一种NOSQL数据库,Key-Value形式对数据进行存储,其中数据可以以内存形式存在,也可以持久化到文件系统.Spring data对Redis ...
- 【Spring】17、spring cache 与redis缓存整合
spring cache,基本能够满足一般应用对缓存的需求,但现实总是很复杂,当你的用户量上去或者性能跟不上,总需要进行扩展,这个时候你或许对其提供的内存缓存不满意了,因为其不支持高可用性,也不具备持 ...
- Spring Boot + Mybatis + Redis二级缓存开发指南
Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...
- 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署 ClassCastException异常 反射 ...
- (转)spring boot整合redis
一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...
随机推荐
- cbv装饰器 中间件 跨站请求伪造
给cbv下面的函数加装饰器 写一个验证用户登录的程序 前端页面 # 写一个装饰器验证session def login_auth(func): def inner(request,*args,**kw ...
- Hadoop_30_MapReduce_多job串联
一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: 每个job装配完成才可以进行下面代码: Cont ...
- Python 实现快递查询
实现效果: 源代码: import urllib.request import json import msvcrt kd_dict = {1:'shentong',2:'youzhengguonei ...
- springMVC的简单了解和环境搭建
一,什么mvc 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计思想.它主要通过 分离模型.视图及控制器在应用程序中的角色 将业务逻辑从界面中解耦.通常, 模型负责封装应用程 ...
- jsp的标签库
Java Server Pages Standard Tag Libray(JSTL):JSP 标准标签库,是一个定制标签类库的集合,用于解决一些常见的问题,例如迭代一个映射或者集合.条件测试.XML ...
- 《黑白团团》第九次团队作业:Beta冲刺与验收准备
项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 GitHub链接 Scrum meeting导航 [Beta]Scrum m ...
- Spring Boot 前期篇
在学习springboot之前,学习一下Spring的java配置. 1. Spring的发展 1.1. Spring1.x 时代 在Spring1.x时代,都是通过xml文件配置bean,随着项目的 ...
- ibatis查询列表跟总记录,都引用相同SQL
在查询记录集合跟查询记录总记录数的时候,我们需要所写的SQL要一样,那么可以都引用同一个SQL.写法如下: <sqlMap namespace="Server"> &l ...
- springboot2.0入门(五)--swagger2接口API构建
一.特点 代码变,文档变.只需要少量的注解,Swagger 就可以根据代码自动生成 API 文档,很好的保证了文档的时效性. 跨语言性,支持 40 多种语言. Swagger UI 呈现出来的是一份可 ...
- Can't load Microsoft.ReportViewer.ProcessingObjectModel.dll
本机的时候是能正常看到report,但deploy到别的机器上却不行,按说从本机拷个dll过去就可以,但怎么也找不到. 原来要在cmd那里输入C:\WINDOWS\assembly\GAC_MSIL ...