Spring中使用Redis
普通使用Redis的方法很简单,前面的文章也有讲解,这篇文章主要就是讲解通过注解的方式实现Spring和Redis的整合。
这里我们创建了3个类:
1.Config 全局配置类,相当于xml配置文件
2.RedisTest 启动类,就一个main方法,同时初始化Spring容器
3.SpringStart 具体操作类,在这个类里面操作具体的Redis
pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
config
package cn.duanjt; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration
public class Config {
@Bean("poolConfig")
public JedisPoolConfig JedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(100);
config.setTestOnBorrow(true);
return config;
//return new JedisPool(config, "172.23.88.107", 6379);
} @Bean("jedisConnectionFactory")
public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig poolConfig){
JedisConnectionFactory factory=new JedisConnectionFactory(); factory.setHostName("172.23.88.107");
factory.setPort(6379);
factory.setPoolConfig(poolConfig);
factory.setUsePool(true);
return factory;
} @Bean("redisTemplate")
public RedisTemplate<String, Object> RedisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template=new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);//开启事务
return template;
} @Bean
public SpringStart SpringStart(){
return new SpringStart();
}
}
RedisTest
package cn.duanjt; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class RedisTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
SpringStart test=context.getBean(SpringStart.class);
test.start();
} }
SpringStart
package cn.duanjt; import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; public class SpringStart {
@Autowired
RedisTemplate<String, Object> redisTemplate; public void start() {
// 设置值
redisTemplate.opsForValue().set("age", 100);
// 获取值
System.out.println("String年龄"+redisTemplate.opsForValue().get("age")); Map<String, String> map = new HashMap<>();
map.put("age", "100");
map.put("name", "张三");
redisTemplate.opsForHash().putAll("stu:001", map); System.out.println("Hash姓名:"+redisTemplate.opsForHash().get("stu:001", "age"));
//redisTemplate.opsForSet();
//redisTemplate.opsForZSet();
}
}
1.注意最后的redisTemplate.opsForSet()和redisTemplate.opsForZSet(),Redis里面的不同类型对应了一个不同方法
2.开始启动的时候报了类没有找到的错误,后来排查是因为jedis和spring-data-redis的版本不兼容,后来将jedis修改为2.9.0之后完美解决。
Spring中使用Redis的更多相关文章
- 记自己在spring中使用redis遇到的两个坑
本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...
- spring中订阅redis键值过期消息通知
1.首先启用redis通知功能(ubuntu下操作):编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知): notify-keyspace-events Ex 或者登陆 ...
- 在Spring中使用Redis Lua脚本批量删除缓存
背景 之前分享了一篇利用lua脚本批量删除redis的key的文章.现在项目中我打算使用spring的缓存,而Spring缓存以前我是用ehcache来做实现的.没发现什么问题..这次我换成redis ...
- spring中添加redis缓存
1.单机版的添加 spring里面配置 <bean id="redisClient" class="redis.clients.jedis.JedisPool&qu ...
- 在SpringBoot中引入Redis
前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...
- Spring+Dubbo集成Redis的两种解决方案
当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...
- Spring下使用Redis
在Spring中使用Redis使用使用两个依赖包jedis.jar.spring-data-redis.jar 一下是Maven项目pom.xml添加依赖 <!--jedis.jar --> ...
- 浅析redis缓存 在spring中的配置 及其简单的使用
一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...
随机推荐
- GitHub 设置首页显示 404 There isn't a GitHub Pages site here.
问题如题! 能使用的必要条件是: 1.创建的仓库 Code 中 必须 有 README.md 文件,内容自定 2.设置模板在仓库中 Settings -->GitHub Pages --> ...
- xilink 烧写flash
no 右键
- 程序员调 Bug 的样子,非常真实
程序员调 Bug 的样子,非常真实
- 复习C++:VS2008中的宏干嘛用的
VS2008中有宏,可也进行编辑和设置. 好处: 1.快速生成代码,帮助开发. 2.个人定制化功能,IDE更合适自己用. 3.提升编程效率. 缺点: 使用VB开发,一开始用起来费事.不过有官方参考 ...
- [Day15]常用API(Object类、String类)
1.Java的API(API: Application(应用) Programming(程序) Interface(接口)) Java API是JDK中提供使用的类,类已经将底层代码进行封装 在JDK ...
- 洛谷P4324 扭动的回文串 [JSOI2016] manacher+哈希
正解:manacher+哈希 解题报告: 传送门 要不要先解释下题意,,,我开始看了半天来着QAQ 大概就,要求一个最长的回文串 这个回文串有两种构成可能 一种是单从一个串中拿出来的连续一段 另一种是 ...
- python终端打印带颜色的print
原理 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示 ...
- grunt 打包 分解(并非原创)
1. require('time-grunt')(grunt); Time how long tasks take. Can help when optimizing build times任务执行时 ...
- IT人员如何开好站立会议
一.来由 软件开发的过程却又是一个离不开协作.沟通的过程.一个缺乏良好协作,沟通.理解和目标一致的软件团队,是很难高质高效的交付的. 敏捷的众多实践中,有一个为了提升团队协作的经典实践:站立会议 二. ...
- MongoDB 知识点
左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...