SpringBoot环境

快速搭建一个SpringBoot工程

进入 https://start.spring.io 网站, 使用该网站初始化一个SpringBoot工程

添加相关依赖

因为使用spring initializer已经帮我们把Redis的依赖建立好了; 但是由于我们要使用Jedis客户端访问Redis, 所以还需要添加Jedis的依赖;

<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.9.0</version> //版本号可以放在properties中作为属性, 这边用${jedis.version}来依赖
 </dependency>

配置Redis节点信息

打开application.properties文件, 初始化的文件是空的; 我们将spring redis最基本的信息加入进去

spring.redis.host=localhost
spring.redis.port=6379

将Redis信息读入到程序中

新建一个Java类命名为 StandaloneRedisConfig.java , 放在 com.xxx.example.config 包下

package com.terrylmay.redis.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@ConditionalOnProperty(name = {"spring.redis.host"})
public class StandaloneRedisConfig {
 String host;
 int port;
 public String getHost() {
 return host;
 }
 public void setHost(String host) {
 this.host = host;
 }
 public int getPort() {
 return port;
 }
 public void setPort(int port) {
 this.port = port;
 }
}

上面配置中的 @ConditionalOnProperty(name = {"spring.redis.host"}) 如果只是单机的Redis则不需要添加该属性; 但是为了后面一套代码兼容多个Redis部署模式, 使用该属性作为是否创建Bean的条件; 如果是集群模式那么就不会使用 spring.redis.host 来作为连接字符串了;

配置Jedis的连接池

将Redis连接对象放入到Spring容器中进行管理

package com.terrylmay.redis.example;
import com.terrylmay.redis.example.config.StandaloneRedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootApplication(scanBasePackages = {"com.terrylmay.redis.example"})
public class RedisExampleApplication {
 public static void main(String[] args) {
 SpringApplication.run(RedisExampleApplication.class, args);
 }
 @Autowired
 StandaloneRedisConfig standaloneRedisConfig;
 @Autowired
 RedisConnectionFactory redisConnectionFactory;
 @Bean
 @ConditionalOnBean(value = {StandaloneRedisConfig.class})
 public RedisConnectionFactory standaloneRedisConnectionFactory() {
 JedisConnectionFactory factory = new JedisConnectionFactory(new RedisStandaloneConfiguration(standaloneRedisConfig.getHost(), standaloneRedisConfig.getPort()));
 return factory;
 }
 @Bean
 public StringRedisTemplate stringRedisTemplate() {
 return new StringRedisTemplate(redisConnectionFactory);
 }
}

这里的 @ConditionalOnBean(value = {StandaloneRedisConfig.class}) 与上面的 ConditionalOnProperty 是一个道理

这里的 scanBasePackages = {"com.terrylmay.redis.example"} 是为了以后将Redis的客户端独立出一个工程而做的, 当然独立出来的工程base包名还要是这个才可以;

因为还没有看Redis支持的数据结构, 那么现在只是把Redis字符串模板类放到Spring 容器中, 后续再增加其他数据类型的支持;

创建操作Redis的接口 以及实现

创建 ICacheProvider.java 接口:

package com.terrylmay.redis.example.provider;
public interface ICacheProvider {
 void setString(String key, String value);
 String getString(String key);
}

Jedis版本的实现:

package com.terrylmay.redis.example.provider.impl;
import com.terrylmay.redis.example.provider.ICacheProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class JedisCacheProvider implements ICacheProvider {
 @Autowired
 StringRedisTemplate stringRedisTemplate;
 @Override
 public void setString(String key, String value) {
 stringRedisTemplate.opsForValue().set(key, value);
 }
 @Override
 public String getString(String key) {
 return stringRedisTemplate.opsForValue().get(key);
 }
}

这样基本上一个可以操作Redis的Java程序就已经就绪了; 那么我们需要验证一下, 当然如果在主工程中写一个类去验证也是没有问题的, 比如创建一个Bean, 并且放到被 PostContruct 注解的方法里面;

但是更加专业的做法是写一个测试程序来测试, 下面看一下该测试程序应该怎么写

UT测试程序可用性

因为创建工程的时候, 就已经有一个测试类在test目录下面了, 我们增加我们想要的功能

package com.terrylmay.redis.example;
import com.terrylmay.redis.example.provider.ICacheProvider;
import org.junit.Assert;
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.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {RedisExampleApplication.class})
public class RedisExampleApplicationTests {
 @Autowired
 ICacheProvider jedisCacheProvider;
 @Test
 public void contextLoads() {
 jedisCacheProvider.setString("name", "terrylmay");
 System.out.println(jedisCacheProvider.getString("name"));
 Assert.assertEquals("terrylmay", jedisCacheProvider.getString("name"));
 }
}

注: 程序中不要有打印, 使用Logger或者直接断言来处理 (本来想用markdown语法来标红的, 但是发现简书竟然不支持html的写法; 没办法只能用``来搞定了)

开发过程中遇到的问题

一、在写好所有的程序之后, 跑测试用例, 但是始终都是报NoSuchBeanException

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.terrylmay.redis.example.config.StandaloneRedisConfig' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {
@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因共有三点:

1、写了 scanBasepackages 来扫描包下面的bean, 扫描的包与类所在的包不一样, 只有一个字符之差 com.terrylmay.redis.example 与 com.terrlmay.redis.example , 当然这时候 idea 会报错, 只是我不认识那个错而已; Idea报错如图所示:

2、按照网上的application.properties属性的读取方式, 只使用了一个注解:

@ConfigurationProperties(prefix = "spring.redis") 但是点进该注解里面看, 它其实并没有Component注解的功能; 所以增加了 @Configuration 注解

3、第三个原因不仔细断然不会发现这个错误

我理解的是只要工程里面父工程是 spring-boot-starter-parent , 那么就不应该存在这类Jar包没有依赖的问题, 打开文档

依赖可粘贴版:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
 <optional>true</optional>
</dependency>

到这里, 一个能够使用Redis的Java工程就已经就绪了

深入理解Redis系列之SpringBoot集成Redis的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  4. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  5. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  6. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  7. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  8. Redis 系列(04-2)Redis原理 - 内存回收

    目录 Redis 系列(04-2)Redis原理 - 内存回收 Redis 系列目录 1. 过期策略 1.1 定时过期(主动淘汰) 1.2 惰性过期(被动淘汰) 1.3 定期过期 2. 淘汰策略 2. ...

  9. Redis系列(七)Redis面试题

    Redis 系列: Redis系列(一)Redis入门 Redis系列(二)Redis的8种数据类型 Redis系列(三)Redis的事务和Spring Boot整合 Redis系列(四)Redis配 ...

随机推荐

  1. Integer类toString(int i,int radix)方法

    Integer类toString(int i,int radix)方法: 首先抛出java的api中的介绍: public static String toString(int i, int radi ...

  2. 如何使用postman传数组数据

    如何使用postman传数组数据 在我们做api接口数据调试的时候,大部分是会用到postman的,一般请求数据的参数都是字符串,但是特殊情况下我们是需要传一个数组数据的,那么为了实现这种需求,究竟该 ...

  3. 承接AR摄像头识别外包 AR图像识别 AR识别图像 AR识别应用外包

    增强现实简称AR,是一种实时计算摄影机影像的位置及角度并加上相应图像的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并实现互动. 这里千万别和VR虚拟现实混了.简单说就是VR看到的场景都是假的, ...

  4. Xilinx Vivado的使用详细介绍(3):使用IP核

    ilinx Vivado的使用详细介绍(3):使用IP核 Author:zhangxianhe IP核(IP Core) Vivado中有很多IP核可以直接使用,例如数学运算(乘法器.除法器.浮点运算 ...

  5. P2822 组合数问题

    传送门 思路: 利用公式: C( n,r ) = C( n-1,r ) + C( n-1,r-1 ) 由此可以将计算 C( n,r ) 的过程化为加法来做. 可以看出,C( n,r ) 其实就是求杨辉 ...

  6. 环境搭建之allure的安装配置,及简单使用

    环境准备 首先是要安装好jdk的电脑上,运行java.javac这些命令都没有问题,要不安装allure时会报错 下载allure 如果直接用Jenkins上的插件,并不需要下载安装 allure官网 ...

  7. js ++i和i++的区别

    ++i和i++的定义: 1.  如果用前缀运算符对一个变量增1(减1),则在将该变量增1(减1)后,用新值在表达式中进行其他的运算.    2. 如果用后缀运算符对一个变量增1(减1),则用该变量的原 ...

  8. 【转载】Java性能优化之JVM GC(垃圾回收机制)

    文章来源:https://zhuanlan.zhihu.com/p/25539690 Java的性能优化,整理出一篇文章,供以后温故知新. JVM GC(垃圾回收机制) 在学习Java GC 之前,我 ...

  9. scrapy框架使用笔记

    目前网上有很多关于scrapy的文章,这里我主要介绍一下我在开发中遇到问题及一些技巧: 1,以登录状态去爬取(带cookie) -安装内容: brew install phantomjs (MAC上) ...

  10. sunset

    may there be enough clouds in your life to make a beautiful sunset