SpringBoot集成Redis的實現(xiàn)示例
前面一篇文章已經(jīng)寫了如何搭建一個單機版Redis服務, 那么我們應該怎么在現(xiàn)有的系統(tǒng)中集成進來呢? 由于筆者使用的編程語言是Java, 所以本篇文章主要描述SpringBoot如何集成單Redis節(jié)點完成數(shù)據(jù)的增刪改查.
SpringBoot環(huán)境
快速搭建一個SpringBoot工程
進入 https://start.spring.io
網(wǎng)站, 使用該網(wǎng)站初始化一個SpringBoot工程
添加相關(guān)依賴
因為使用spring initializer已經(jīng)幫我們把Redis的依賴建立好了; 但是由于我們要使用Jedis客戶端訪問Redis, 所以還需要添加Jedis的依賴;
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> //版本號可以放在properties中作為屬性, 這邊用${jedis.version}來依賴 </dependency>
配置Redis節(jié)點信息
打開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部署模式, 使用該屬性作為是否創(chuàng)建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支持的數(shù)據(jù)結(jié)構(gòu), 那么現(xiàn)在只是把Redis字符串模板類放到Spring 容器中, 后續(xù)再增加其他數(shù)據(jù)類型的支持;
創(chuàng)建操作Redis的接口 以及實現(xiàn)
創(chuàng)建ICacheProvider.java
接口:
package com.terrylmay.redis.example.provider; public interface ICacheProvider { void setString(String key, String value); String getString(String key); }
Jedis版本的實現(xiàn):
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程序就已經(jīng)就緒了; 那么我們需要驗證一下, 當然如果在主工程中寫一個類去驗證也是沒有問題的, 比如創(chuàng)建一個Bean, 并且放到被PostContruct
注解的方法里面;
但是更加專業(yè)的做法是寫一個測試程序來測試, 下面看一下該測試程序應該怎么寫
UT測試程序可用性
因為創(chuàng)建工程的時候, 就已經(jīng)有一個測試類在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語法來標紅的, 但是發(fā)現(xiàn)簡書竟然不支持html的寫法; 沒辦法只能用``來搞定了)
開發(fā)過程中遇到的問題
一、在寫好所有的程序之后, 跑測試用例, 但是始終都是報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、按照網(wǎng)上的application.properties屬性的讀取方式, 只使用了一個注解:
@ConfigurationProperties(prefix = "spring.redis")
但是點進該注解里面看, 它其實并沒有Component注解的功能; 所以增加了@Configuration
注解
3、第三個原因不仔細斷然不會發(fā)現(xiàn)這個錯誤
我理解的是只要工程里面父工程是spring-boot-starter-parent
, 那么就不應該存在這類Jar包沒有依賴的問題, 打開文檔
依賴可粘貼版:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
到這里, 一個能夠使用Redis的Java工程就已經(jīng)就緒了; 最終的代碼全部在Github的spring-redis-example倉庫下, 歡迎star與PR
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java的main方法中調(diào)用spring的service方式
這篇文章主要介紹了在java的main方法中調(diào)用spring的service方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring攔截器HandlerInterceptor接口代碼解析
這篇文章主要介紹了Spring攔截器HandlerInterceptor接口代碼解析,具有一定借鑒價值,需要的朋友可以參考下2017-12-12