条件:引用好架包

org.springframework.data

spring-data-redis

2.1.3.RELEASE

redis.clients

jedis

2.9.0

一、使用xml进行配置

1、xml进行配置JedisPoolConfig、JedisConnectionFactory、Spring RedisTemplate-

2、使用:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");

RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

redisTemplate.opsForValue().set("key1","value1");

redisTemplate.opsForValue().set("key2","value2");

String value1 = (String) redisTemplate.opsForValue().get("key1");

System.out.println(value1);

二、使用java方式

1、创建RedisConfg配置类

packagecom.wbg.mr.spring;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.serializer.StringRedisSerializer;

@Configurationpublic classRedisConfig {

@BeanpublicJedisConnectionFactory jedisConnectionFactory(){

JedisConnectionFactory jcf= newJedisConnectionFactory();

jcf.setHostName("localhost");returnjcf;

}

@BeanpublicRedisTemplate redisTemplate(){

RedisTemplate rt= newRedisTemplate();

rt.setConnectionFactory(jedisConnectionFactory());

rt.setKeySerializer(newStringRedisSerializer());

rt.setValueSerializer(newStringRedisSerializer());returnrt;

}

}

2、使用

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RedisConfig.class);

RedisConfig redisConfig= applicationContext.getBean(RedisConfig.class);

RedisTemplate redisTemplate=redisConfig.redisTemplate();

redisTemplate.opsForValue().set("key11","value11");

redisTemplate.opsForValue().set("key12","value12");

String value11= (String) redisTemplate.opsForValue().get("key11");

System.out.println(value11);

测试:

packagecom.wbg.mr.spring;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.data.redis.core.RedisTemplate;public classMain {public static voidmain(String[] args) {

annotationConfigApplicationContext();

}public static voidclassPathXmlApplicationContext(){

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("application.xml");

RedisTemplate redisTemplate= applicationContext.getBean(RedisTemplate.class);

redisTemplate.opsForValue().set("key1","value1");

redisTemplate.opsForValue().set("key2","value2");

String value1= (String) redisTemplate.opsForValue().get("key1");

System.out.println(value1);

}public static voidannotationConfigApplicationContext(){

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(RedisConfig.class);

RedisConfig redisConfig= applicationContext.getBean(RedisConfig.class);

RedisTemplate redisTemplate=redisConfig.redisTemplate();

redisTemplate.opsForValue().set("key11","value11");

redisTemplate.opsForValue().set("key12","value12");

String value11= (String) redisTemplate.opsForValue().get("key11");

System.out.println(value11);

}

}

View Code

redis java spring_spring配置redis(xml+java方式)(最底层)相关推荐

  1. Redis安装与配置Redis安装与配置

    今天在使用Redis的时候遇到了一些问题,这个问题的解决,发现很多人使用Redis的时候没有一点安全意识.所以又重温了一下Redis,觉得应该写一下Redis的安全和配置. Redis安装与配置Red ...

  2. 咳咳,会用redis的帅哥拿到了上万,我这会用redis,还会配置redis的小鲜肉最低也待18k吧

    刚刚看到个会使用redis的帅哥拿到了上万,我很是不服,我这不但会使用redis还会配置redis,而且会redis集群,主从复制,哨兵模式和解决缓存穿透.缓存击穿和缓存雪崩等问题,重点重点我还是小鲜 ...

  3. spring java code配置_Spring-09-使用Java的方式配置Spring

    9. 使用Java的方式配置Spring 我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring! JavaConfig是Spring的一个子项目,在Spring4之后,他成 ...

  4. 实战SSM_O2O商铺_45【Redis缓存】配置Redis在Service层加入缓存

    文章目录 概述 Windows下安装Redis O2O-Service层加入缓存的配置步骤 1. pom.xml 添加jedis依赖包 2. redis配置文件 3.spring-dao.xml加载r ...

  5. Springboot+Redis+阿里云服务器 Redis是什么?如何配置Redis?怎样配置Redis?如何按装Redis?

    如何在SpringBoot项目使用Redis存储session 一.什么是Redis? 二.此次案例使用Redis做什么? 三.阿里云服务器安装Redis 四.SpringBoot项目添加yml配置 ...

  6. java环境变量设置 win2003,2021-03-09Win10的Java环境配置Win10下Java环境变量配置

    接下来主要讲怎么配置 Java 的环境变量,也是为了以后哪天自己忘记了做个备份 (注:win10的Java环境变量配置和其他的windows版本稍有不同) 在电脑桌面 右键点击 "此电脑&q ...

  7. redis3.0 java spring_spring整合redis以及使用RedisTemplate的方法

    需要的jar包 spring-data-Redis-1.6.2.RELEASE.jar jedis-2.7.2.jar(依赖 commons-pool2-2.3.jar) commons-pool2- ...

  8. corn java 可配置_SpringBoot——目前Java开发最流行的框架(四)

    [SpringBoot] 十九.SpringBoot整合Junit4 1.添加启动器 2.编写测试类 在src/main/test里面新建com.bjsxt.MyTest 注意: 测试类不能叫做Tes ...

  9. java 原生 json 转 xml java json转xml 实现代码 读取本地json文件 转成 xml数据 用springboot 发布xml格式数据

    结果xml截图 json数据 main启动 import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; ...

最新文章

  1. Facial keypoints detection Kaggle 竞赛系列
  2. stream获取filter
  3. 全球电动车竞争加剧 特斯拉全球市场份额持续下滑
  4. Spring的注解问题
  5. c++ 结构体地址 转换成ulong_Nicole_coder
  6. NSURLConnection-网络访问(同步异步)
  7. VS2015sql本地服务器为空,详解VS2015自带LocalDB数据库用法实例
  8. DOM方式操作元素属性
  9. asp毕业设计——基于asp+access的公司门户网站设计与实现(毕业论文+程序源码)——公司门户网站
  10. View系列:事件分发:滑动触摸事件分发
  11. 编程猫李天驰:让编程教育回归互联网
  12. [luogu]P1852跳跳棋
  13. Python股票数据爬虫解读
  14. 文案排版(参考中文文案排版指北)
  15. 根据起始时间,获取之间所有的时间(基于momentjs)
  16. 51单片机实战教程之C语言基础(一 创建Keil Project)
  17. Android 编译 Jack简介
  18. Python format方法详解|字符串格式化|format
  19. 匹兹堡大学申请条件计算机科学,匹兹堡大学计算机科学排名第62(2020年TFE美国排名)...
  20. ADIS16465姿态解算+卡尔曼滤波代码

热门文章

  1. PHP中刷新输出缓冲
  2. 目前网络上开源的网络爬虫以及一些简介和比较
  3. HDUOJ----1166敌兵布阵(线段树单点更新)
  4. POI处理Excel中的日期数据类型
  5. Java基础篇:多线程
  6. OAuth 2 实现单点登录,通俗易懂!
  7. 微服务平台的发展趋势
  8. 梳理消息队列 MQ/JMS/Kafka
  9. 新款笔记本写代码贼爽,包邮送一台!
  10. 阿里某P5程序员求助:跟女票要结婚,她家要50万彩礼,女票爸爸说钱不够可以先欠着,这婚能结吗?欠条以后能赖吗?...