普通使用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. stm32专属于菜鸟的学习方法

    1.首先我们先看看与STM32相关的文档 我们假定大家已经对STM32的书籍或者文档有一定的理解.如不理解,请立即阅读STM32的文档,以获取最基本的知识点. 如果你手上拥有ST官方主推的STM32神 ...

  2. PE、PB、PEG三大估值法的正确使用方法!

    目前市面上的估值方法有很多,比如PE估值法.PB估值法.PEG估值法,但是我相信,真正会用的人并不多,比如说目前动态市盈率121倍的比亚迪真的高估吗?比如目前市净率为0.63倍的众泰汽车真的是破净股吗 ...

  3. 15.1-uC/OS-III资源管理(锁调度器)

    1.大部分独占资源的方法都是创建临界段:1) 关中断方式2) 锁调度器方式3) 信号量方式4) mutex方式 2.独占共享资源的最快和最简单方法是关中断 然而,关/开中断是和CPU相关的操作,其相关 ...

  4. MongoDB "$" 字符 下标位置

    我们可以修改列表里面元素的名字 例如: 修改age=34的数据,hobby里面的"足球"改为"网球" }) { "_id" : Object ...

  5. wordpress调用the_excerpt()不带<p>标签

    我们知道wordpress调用摘要内容用<?php the_excerpt(); ?>就可以,但是它会自动添加一个p标签,例如<p>这里是description</p&g ...

  6. python框架之Django(14)-rest_framework模块

    APIView django原生View post请求 from django.shortcuts import render, HttpResponse from django import vie ...

  7. Win10系统jdk环境变量配置方法

    http://www.w10zj.com/Win10xy/Win10yh_5620.html

  8. Linux环境下使用tcpdump抓包与下载

    (1)报文抓取 tcpdump -i eno5 host 10.8.12.154 -w /test.cap -i:抓取的网卡 host:目的地址 -w:生成的文件存放路径 Ctrl+c 结束抓包,抓取 ...

  9. airflow中的两个参数

    'trigger_rule':'all_done','retry_delay':timedelta(),

  10. [ Python ] OpenGL

    pyOpenGL Installation Package Version------------------- -------numpy 1.14.2 PyOpenGL 3.1.0 PyOpenGL ...