对于单元测试来说,我们应该让它尽量保持单一环境,不要与网络资源相通讯,这样可以保证测试的稳定性与客观性,对于springboot这个框架来说,它集成了单元测试JUNIT,同时在设计项目时,你可以使用多种内嵌的存储工具,像mongodb,redis,mysql等等,今天主要说一下embedded-redis的使用。

添加包引用build.gradle

  1. testCompile(
  2. 'com.github.kstyrc:embedded-redis:0.6'
  3. )

添加配置注入

  1.  
  1. import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.SetOperations;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.core.ZSetOperations;
    import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
  1. @Configuration
  2. public class RedisConfig {
  3. /**
  4. * 注入 RedisConnectionFactory
  5. */
  6. @Autowired
  7. RedisConnectionFactory redisConnectionFactory;
  8.  
  9. /**
  10. * 实例化 RedisTemplate 对象
  11. *
  12. * @return
  13. */
  14. @Bean
  15. public RedisTemplate<String, Object> functionDomainRedisTemplate() {
  16. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  17. initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
  18. return redisTemplate;
  19. }
  20.  
  21. /**
  22. * 设置数据存入 redis 的序列化方式
  23. *
  24. * @param redisTemplate
  25. * @param factory
  26. */
  27. private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
  28. redisTemplate.setKeySerializer(new StringRedisSerializer());
  29. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  30. redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
  31. redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
  32. redisTemplate.setConnectionFactory(factory);
  33. }
  34.  
  35. /**
  36. * 实例化 HashOperations 对象,可以使用 Hash 类型操作
  37. *
  38. * @param redisTemplate
  39. * @return
  40. */
  41. @Bean
  42. public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
  43. return redisTemplate.opsForHash();
  44. }
  45.  
  46. /**
  47. * 实例化 ValueOperations 对象,可以使用 String 操作
  48. *
  49. * @param redisTemplate
  50. * @return
  51. */
  52. @Bean
  53. public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
  54. return redisTemplate.opsForValue();
  55. }
  56.  
  57. /**
  58. * 实例化 ListOperations 对象,可以使用 List 操作
  59. *
  60. * @param redisTemplate
  61. * @return
  62. */
  63. @Bean
  64. public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
  65. return redisTemplate.opsForList();
  66. }
  67.  
  68. /**
  69. * 实例化 SetOperations 对象,可以使用 Set 操作
  70. *
  71. * @param redisTemplate
  72. * @return
  73. */
  74. @Bean
  75. public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
  76. return redisTemplate.opsForSet();
  77. }
  78.  
  79. /**
  80. * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
  81. *
  82. * @param redisTemplate
  83. * @return
  84. */
  85. @Bean
  86. public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
  87. return redisTemplate.opsForZSet();
  88. }
  89. }

在业务层中使用redis

  1. @Autowired
  2. RedisTemplate<String, Object> redisCacheTemplate;

在使用过程中,我们的RedisTemplate对象已经被Autowired注入了。

感谢阅读!

springboot~内嵌redis的使用的更多相关文章

  1. 查看和指定SpringBoot内嵌Tomcat的版本

    查看当前使用的Tomcat版本号 Maven Repository中查看 比如我们需要查Spring Boot 2.1.4-RELEASE的内嵌Tomcat版本, 可以打开链接: https://mv ...

  2. SpringBoot内嵌Tomcat开启APR模式(运行环境为Centos7)

    网上查到的一些springboot内嵌的tomcat开启apr的文章,好像使用的springboot版本较老,在SpringBoot 2.0.4.RELEASE中已经行不通了.自己整理了一下,供参考. ...

  3. spring-boot内嵌三大容器https设置

    spring-boot内嵌三大容器https设置 spring-boot默认的内嵌容器为tomcat,除了tomcat之前还可以设置jetty和undertow. 1.设置https spring-b ...

  4. SpringBoot内嵌数据库的使用(H2)

    配置数据源(DataSource) Java的javax.sql.DataSource接口提供了一个标准的使用数据库连接的方法. 传统做法是, 一个DataSource使用一个URL以及相应的证书去构 ...

  5. SpringBoot 内嵌容器的比较

    Spring Boot内嵌容器支持Tomcat.Jetty.Undertow.为什么选择Undertow? 这里有一篇文章,时间 2017年1月26日发布的: 参考 Tomcat vs. Jetty ...

  6. SpringBoot内嵌ftp服务

    引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.ftpserver/ftpserver-core --> <depen ...

  7. Spring Boot启动过程(五):Springboot内嵌Tomcat对象的start

    标题和上一篇很像,所以特别强调一下,这个是Tomcat对象的. 从TomcatEmbeddedServletContainer的this.tomcat.start()开始,主要是利用Lifecycle ...

  8. maven打包排除spring-boot内嵌tomcat容器依赖jar

    在pom文件中添加打包排除配置信息. <plugin> <artifactId>maven-war-plugin</artifactId> <version& ...

  9. springboot内嵌定时任务使用及cron表达式讲解

    第一步:pom引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

随机推荐

  1. go语言 nsq源码解读四 nsqlookupd源码options.go、context.go和wait_group_wrapper.go

    本节会解读nsqlookupd.go文件中涉及到的其中三个文件:options.go.context.go和wait_group_wrapper.go. options.go 123456789101 ...

  2. Linux下网站根目录权限

    网站根目录权限遵循: 文件644 文件夹755 权限用户和用户组www-data 如出现文件权限问题时,请执行下面3条命令: chown -R www-data.www-data /usr/local ...

  3. 处理php出现default timezone抖动的问题

    懒癌发作1年多,再次回来写随笔.(上次是16年,再上次是13年,好像懒的没救了) 这回遇到一个系统前端展现的时间在无规律抖动的问题: 前端php环境是5.3,运行于apache上,php.ini中已经 ...

  4. Postman----安装Newman

    Newman是为Postman而生,专门用来运行Postman编写好的脚本.Newman安装步骤: 1.需要安装node.js,并配置好环境 2.打开控制台,运行:npm install -g new ...

  5. selenium+python,解决selenium弹出新页面,无法定位元素的问题(报错:Unable to locate element:元素)

    1.问题发生描述: 从一个页面进行点击等操作,页面跳转到第二个页面,对第二个页面中的元素,采取任何措施定位都报错,问题报错点如下: 2.出现问题的原因: 窗口句柄还停留在上一个页面,对于当前新弹出的页 ...

  6. python环境下实现OrangePi Zero寄存器访问及GPIO控制

    最近入手OrangePi Zero一块,程序上需要使用板子上自带的LED灯,在网上一查,不得不说OPi的支持跟树莓派无法相比.自己摸索了一下,实现简单的GPIO控制方法,作者的Zero安装的是Armb ...

  7. css 滚动视差 之 水波纹效果

    核心属性: background-attachment 这个属性就牛逼了, 它可以定义背景图片是相对视口固定, 还是随着视口滚动, 加上这个属性网页瞬间就从屌丝变成 高大上. 我们来看个例子: htm ...

  8. 流程控制之if判断

    目录 语法(掌握) if if...else if...elif...else 练习(掌握) 练习1:成绩评判 练习2:模拟登录注册 if的嵌套(掌握) 语法(掌握) if判断是干什么的呢?if判断其 ...

  9. requests使用“proxy”代理访问接口

    在requests中使用proxy代理访问 使用前先更新requests版本为支持socks的版本.   先pip安装对应库:  >> pip install -U requests[so ...

  10. Spring boot 继承 阿里 autoconfig 配置环境参数

    前提:基于springboot 项目 1. 配置pom.xml 文件 <plugin> <groupId>com.alibaba.citrus.tool</groupId ...