在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. asp.net 页面静态化

    页面静态化,有三种方式 伪静态  真静态,折中法  现在我做的是折中发 创建一个asp.net  页面,  连接跳转到还未生成的页面 创建HttpHandle类 using System;using ...

  2. Hadoop_28_MapReduce_自定义 inputFormat

    1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...

  3. java—多线程—notify/notifyAll

    notify应该使用在,只有两个线程在调用类中的两个方法,并在两个方法中进行互斥操作? 博客:Java多线程学习之wait.notify/notifyAll 详解 未完待续……

  4. Paper Reading:Deep Neural Networks for Object Detection

    发表时间:2013 发表作者:(Google)Szegedy C, Toshev A, Erhan D 发表刊物/会议:Advances in Neural Information Processin ...

  5. 自定义系统类加载器源码分析与forName方法底层剖析

    基于上一次[https://www.cnblogs.com/webor2006/p/9240898.html]Launcher的分析继续,上次分析到了这: 接着创建应用类加载器,其创建过程其扩展类加载 ...

  6. Python 列出 windows 安装的软件

    Python 列出 windows 安装的软件 参考链接:https://stackoverflow.com/questions/802499/how-can-i-enumerate-list-all ...

  7. string::capacity string::size string::length string::max_size

    size_t capacity() const noexcept; #include <iostream>#include <string> using namespace s ...

  8. gitlab常用的命令

    git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态git stash  #把所有没有提交的修改暂存到stash里面.可用git stash pop回复.git reset ...

  9. 题解 [CF525D] Arthur and Walls

    题面 解析 首先考虑将一个\('*'\)变成\('.'\)后会形成什么, 显然至少是一个\(2\times 2\)的矩形. 因为\(1\times 1\)和\(1\times 2\)的改了没用啊, 而 ...

  10. JSP大文件分片上传

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...