一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot集成Redis的實現示例

SpringBoot集成Redis的實現示例

2021-06-17 11:38terryKing1992 Java教程

這篇文章主要介紹了SpringBoot集成Redis的實現示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前面一篇文章已經寫了如何搭建一個單機版redis服務, 那么我們應該怎么在現有的系統中集成進來呢? 由于筆者使用的編程語言是java, 所以本篇文章主要描述springboot如何集成單redis節點完成數據的增刪改查.

springboot環境

快速搭建一個springboot工程

進入 https://start.spring.io 網站, 使用該網站初始化一個springboot工程

SpringBoot集成Redis的實現示例

添加相關依賴

因為使用spring initializer已經幫我們把redis的依賴建立好了; 但是由于我們要使用jedis客戶端訪問redis, 所以還需要添加jedis的依賴;

?
1
2
3
4
5
<dependency>
     <groupid>redis.clients</groupid>
     <artifactid>jedis</artifactid>
     <version>2.9.0</version> //版本號可以放在properties中作為屬性, 這邊用${jedis.version}來依賴
  </dependency>

配置redis節點信息

打開application.properties文件, 初始化的文件是空的; 我們將spring redis最基本的信息加入進去

?
1
2
spring.redis.host=localhost
spring.redis.port=6379

將redis信息讀入到程序中

新建一個java類命名為standaloneredisconfig.java, 放在com.xxx.example.config包下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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容器中進行管理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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接口:

?
1
2
3
4
5
6
7
package com.terrylmay.redis.example.provider;
 
public interface icacheprovider {
  void setstring(string key, string value);
 
  string getstring(string key);
}

jedis版本的實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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目錄下面了, 我們增加我們想要的功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.examplecom.terrlmay.redis.example, 當然這時候idea會報錯, 只是我不認識那個錯而已; idea報錯如圖所示:

SpringBoot集成Redis的實現示例

2、按照網上的application.properties屬性的讀取方式, 只使用了一個注解:
@configurationproperties(prefix = "spring.redis") 但是點進該注解里面看, 它其實并沒有component注解的功能; 所以增加了@configuration注解

3、第三個原因不仔細斷然不會發現這個錯誤

SpringBoot集成Redis的實現示例

我理解的是只要工程里面父工程是spring-boot-starter-parent, 那么就不應該存在這類jar包沒有依賴的問題, 打開文檔

SpringBoot集成Redis的實現示例

依賴可粘貼版:

?
1
2
3
4
5
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-configuration-processor</artifactid>
  <optional>true</optional>
</dependency>

到這里, 一個能夠使用redis的java工程就已經就緒了; 最終的代碼全部在github的spring-redis-example倉庫下, 歡迎star與pr

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000017151786

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美大片一级片 | 欧美vpswindowssex| 亚洲国产在线2o20 | 91高清国产视频 | 国产成人愉拍免费视频 | 日本午夜大片免费观看视频 | 国产亚洲99影院 | 亚洲AV蜜桃永久无码精品红樱桃 | 污小说在线阅读 | 国产日韩视频一区 | 成人精品第一区二区三区 | 国产盗摄wc女厕所 | 精品一区二区三区五区六区 | 狠狠色96视频| 高清国语自产拍免费视频国产 | 男人的天堂视频在线 | 色综色天天综合网 | 欧美日韩国产中文字幕 | 91亚洲专区 | 免费一级片在线 | 国产精品极品 | 国产成人愉拍免费视频 | www国产精品| 99久视频| 草草视频人人爽 | 国产91精品在线播放 | 午夜在线a亚洲v天堂网2019 | 大学生特黄特色大片免费播放 | 国产精品嫩草影院在线看 | 国色天香社区在线视频播放 | 99久久精品免费看国产高清 | 国产一区二区三区久久精品小说 | 国产三级自拍 | 污黄漫 | 色综合久久夜色精品国产 | 日韩综合久久 | 亚洲视频久久 | 明星ai智能人脸替换造梦在线播放 | 99看视频 | 精品精品国产yyy5857香蕉 | 色婷婷综合久久久中文字幕 |