Spring Boot 实践2 --项目中使用 Redis

背景:基于实践1中,我们使用Redis做为缓存。

(转载请注明来源:cnblogs coder-fang)

  1. POM中加入依赖:

         <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
  2. 在application.properties中加入配置:

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.pool.max-idle=8
    spring.redis.pool.max-active=8
  3. 创建RedisConfig类,并注入RedisTemplate bean:

    package com.test.demo.config;
    
    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.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory)
        {
            StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(factory);
            return stringRedisTemplate;
        }
    }
  4. 实现redisRepostory通用功能:

    package com.test.demo.db.repo.nosql;
    
    import java.util.concurrent.TimeUnit;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class RedisRepository {
    
        @Autowired
        RedisTemplate<String, String> redisTemplate;
    
        public void add(String key,String value) {
            redisTemplate.opsForValue().set(key, value);
        }
        public void add(String key,String value,Long time) {
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
        }
    
        public String get(String key) {
            return redisTemplate.opsForValue().get(key);
        }
    
        public void delete(String key) {
            redisTemplate.opsForValue().getOperations().delete(key);
        }
    
    }
  5. 创建单元测试并运行:

    package com.test.demo;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNull;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.test.demo.db.repo.nosql.RedisRepository;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisTest {
    
        @Autowired
        RedisRepository redisRepo;
    
        @Test
        public void testRedis(){
            redisRepo.add("test", "123", 1L);
            String val = redisRepo.get("test");
            assertEquals(val, "123");
            System.out.println(val);
            val = redisRepo.get("321");
            System.out.println(val);
            assertNull(val);
        }
    }

YES,项目中使用redis已完成。

时间: 2024-07-29 19:36:50

Spring Boot 实践2 --项目中使用 Redis的相关文章

在Spring Boot项目中使用Redis集群

Redis安装 Mac 系统安装Redis brew方式安装 在命令汗执行命令 brew install redis 安装完成之后的提示信息 ==> Downloading https://homebrew.bintray.com/bottles/redis-5.0.2.mojave.bottle.tar.gz ######################################################################## 100.0% ==> Pouring

Spring Boot实践教程:开篇

前言 ??Java项目开发Spring应该是最常被用到的框架了,但是老式的配置方式让人觉得特别的繁琐,虽然可以通过注解去简化xml文件的配置,但是有没有更简单的方式来帮我们完成这些重复性的事情呢?于是Spring Boot就出现了,Spring Boot极大的简化了Spring的应用开发,它采用约定优于配置的方式,让开发人员能够快速的搭建起项目并运行起来. ??我们在项目开发的过程中,总免不了要整合各种框架,类似什么SSM.SSH之类的,这些框架的整合过程是繁琐的,同时又是无聊的,因为大部分情况

Spring Boot 多模块项目创建与配置 (一) (转)

最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些maven多模块配置的知识,但没自己从头到尾创建和配置过,也快忘得差不多了.这次正好对照着这个项目,动手实践一下,下面我们就开始吧. maven多模块项目通常由一个父模块和若干个子模块构成,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块)相互关联.多模块适用于一些比较大的项

五年阿里摸爬滚打,写出这一份Spring boot实践文档

毋庸置疑,Spring Boot在众多从事Java微服务开发的程序员群体中是一个很特别的存在.说它特别是因为它确实简化了基于Spring技术栈的应用/微服务开发过程,使得我们能够很快速地就搭建起一个应用的脚手架并在其上进行项目的开发,再也不用像以前那样使用大量的XML或是注解了,应用在这样的约定优于配置的前提下可以以最快的速度创建出来. 今天就给大家分享五年阿里摸爬滚打,写出的这一份Spring boot实践文档 如果你需要的话可以点赞后[点击我]来获取到 基础应用开发(入门) 1.Spring

Maven 搭建spring boot多模块项目

Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom.xml可被子模块继承,因此项目只是demo,未考虑太多性能问题,所以将诸多依赖 都写在根级`pom.xml`,子模块只需继承就可以使用. 1-3: 根级pom.xml文件在附录1 1-4: 依赖模块 mybatis spring-boot相关模块 2.创建子模块(module) 2-1: file

从零一起学Spring Boot之LayIM项目长成记(一) 初见 Spring Boot

项目背景 之前写过LayIM的.NET版后端实现,后来又写过一版Java的.当时用的是servlet,websocket和jdbc.虽然时间过去很久了,但是仍有些同学在关注.偶然间我听说了SpringBoot这么个东东,据说是省去了很多繁杂的配置.可以傻瓜式的创建项目,轻轻松松做出一个网站来,那么出于我对LayIM的情有独钟,于是乎想借用它来帮助我学习SpringBoot,并且全程记录,省的以后再走弯路和掌握解决问题的方法.(当然,我也是新手,我的解决方法就是百度,stackovreflow等网

从零一起学Spring Boot之LayIM项目长成记(五)websocket

前言 距离上一篇已经比较久的时间了,项目也是开了个头.并且,由于网上的关于Spring Boot的websocket讲解也比较多.于是我采用了另外的一个通讯框架 t-io 来实现LayIM中的通讯功能.本篇会着重介绍我在研究与开发过程中踩过的坑和比较花费的时间的部分. WebSocket 在研究 t-io 的时候,我已经写过关于t-io框架的一些简单例子分析以及框架中关于 websocket 中的编解码代码分析等,有兴趣的同学可以先看一下.因为 在LayIM项目中我会是用到 Showcase D

Spring Boot实践——基础和常用配置

借鉴:https://blog.csdn.net/j903829182/article/details/74906948 一.Spring Boot 启动注解说明 @SpringBootApplication开启了Spring的组件扫描和Spring Boot的自动配置功能.实际上, @SpringBootApplication将三个有用的注解组合在了一起. Spring的@Configuration:标明该类使用Spring基于Java的配置.虽然本书不会写太多配置,但我们会更倾向于使用基于J

Github 上 Star 最多的个人 Spring Boot 开源学习项目

2016年,在一次技术调研的过程中认识到了 Spring Boot ,试用之后便一发不可收拾的爱上它.为了防止学习之后忘记,就在网上连载了 Spring Boot 系列文章,没想到这一开始便与 Spring Boot 深度结缘. 近三年的时间写了一百多篇关于 Spring Boot 的文章(包含两个课程),在写文章的过程中将文中的示例项目托管在 Github 上面,随着学习 Spring Boot 的朋友越来越多,在 Github 上面的关注(Star)人数也越来越多,到现在已经高达 8300