深入理解Redis系列之SpringBoot集成Redis

SpringBoot環境

快速搭建一個SpringBoot工程

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

深入理解Redis系列之SpringBoot集成Redis


添加相關依賴

因爲使用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.SpringJUnit4Cla***unner;
@RunWith(SpringJUnit4Cla***unner.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報錯如圖所示:

深入理解Redis系列之SpringBoot集成Redis


2、按照網上的application.properties屬性的讀取方式, 只使用了一個註解:

@ConfigurationProperties(prefix = "spring.redis") 但是點進該註解裏面看, 它其實並沒有Component註解的功能; 所以增加了 @Configuration 註解

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

深入理解Redis系列之SpringBoot集成Redis


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

深入理解Redis系列之SpringBoot集成Redis


依賴可粘貼版:

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

到這裏, 一個能夠使用Redis的Java工程就已經就緒了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章