普通使用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的更多相关文章

  1. 记自己在spring中使用redis遇到的两个坑

    本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...

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

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

  3. 在Spring中使用Redis Lua脚本批量删除缓存

    背景 之前分享了一篇利用lua脚本批量删除redis的key的文章.现在项目中我打算使用spring的缓存,而Spring缓存以前我是用ehcache来做实现的.没发现什么问题..这次我换成redis ...

  4. spring中添加redis缓存

    1.单机版的添加 spring里面配置 <bean id="redisClient" class="redis.clients.jedis.JedisPool&qu ...

  5. 在SpringBoot中引入Redis

    前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...

  6. Spring+Dubbo集成Redis的两种解决方案

    当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...

  7. Spring下使用Redis

    在Spring中使用Redis使用使用两个依赖包jedis.jar.spring-data-redis.jar 一下是Maven项目pom.xml添加依赖 <!--jedis.jar --> ...

  8. 浅析redis缓存 在spring中的配置 及其简单的使用

    一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...

  9. Spring中使用RedisTemplate操作Redis(spring-data-redis)

    RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...

随机推荐

  1. POJ 2689 - Prime Distance - [埃筛]

    题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...

  2. 类的继承和C3算法

    在Python的新式类中,方法解析顺序并非是广度优先的算法,而是采用C3算法,只是在某些情况下,C3算法的结果恰巧符合广度优先算法的结果. 可以通过代码来验证下: class NewStyleClas ...

  3. Jmeter学习之-http接口功能测试-入门

    ps:默认已经安装好Jmeter工具,配置好相关环境 打开jmeter 工具,为测试计划重新命名 添加线程组:在测试计划上右键,依次选择“添加>Threads>线程组” 添加http请求: ...

  4. python练习题-day9

    2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def dan_index(itter): li=[] for i in range(len(itte ...

  5. CSS-对于IE的兼容问题处理

    css兼容问题 兼容问题 1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白 ...

  6. kafka安装步骤

    kafka 安装内存会报不够 https://stackoverflow.com/questions/9350437/incompatible-initial-and-maximum-heap-siz ...

  7. mongoose findByIdAndUpdate不执行的解决方法

    请参考Mongoose的文档 1.findOneAndUpdate([query], [doc], [options], [callback]) 有callback传递才执行. 2.exec是prom ...

  8. Eclipse集成scala插件

    1.Eclipse中右击help,选择Eclipse Marketplace,搜索scala,一路点击安装,重启Eclipse. 2.新建工程,new->other->出现scala wi ...

  9. 关于mysql处理百万级以上的数据时如何提高其查询速度的方法

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  10. PHP----------php-fpm进程数的一些相关配置

    1.其中pm.max_children如何配置:pm.max_children 数量的多少根据机器内存确定,基本上一个进程需要30M的内存,假设起100个进程,那么就是3000M,3G内存. 2.pm ...