寫本文的原因
1)某大佬問我有沒有自定義過starter?沒有
2)因為不會,是以學習
3) 沒有整合其它技術的小案例不完整,是以選擇了個人認為簡單的redis,自定義myredis-spring-boot -starter
自定義starter的命名規範
SpringBoot提供的starter以
spring-boot-starter-xxx
的方式命名的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.2.2</version>
</dependency>
官方建議自定義的starter使用
xxx-spring-boot-starter
命名規則。以區分SpringBoot生态提供的starter。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
項目結構及介紹
項目結構

項目介紹
1)此項目為定義一個redis整合SpringBoot的starter,為了友善差別用my字首标記自定義的類。
2)myredis-spring-boot-autoconfigure:自定義myredis-starter的核心,核心都在這個module中
3)myredis-spring-boot-starter: 僅僅添加了myredis-spring-boot-autoconfigure的依賴 ,目的是隐藏 細節
4)springboot-demo : 引入自定義的starter依賴,進行測試
項目下載下傳
url:
share/StarterDemo at master · cbeann/share · GitHub
注意:
先install 父工程,
在install 子工程 myredis-spring-boot-autoconfigure
在install 子工程 myredis-spring-boot-starter
在添加依賴在運作測試
項目建構
myredis-spring-boot-autoconfigure子產品(Maven項目)
添加依賴
<!-- 引入spring-boot-starter,所有starter的基本配合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--自定義的yml提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.2.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
連接配接redis的參數配置類
package com.myredis;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author CBeann
* @create 2020-05-30 14:51
*/
@ConfigurationProperties(prefix = "myredis")
public class MyRedisProperties {
//ip
private String host;
//密碼
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
自定義myredisTemplate
package com.myredis;
import redis.clients.jedis.Jedis;
/**
* @author CBeann
* @create 2020-05-30 15:03
*/
public class MyRedisTemplate {
private Jedis jedis;
public MyRedisTemplate(Jedis jedis) {
this.jedis = jedis;
}
public String setString(String key ,String val){
String set = jedis.set(key, val);
return set;
}
public Long delKey(String key){
Long del = jedis.del(key);
return del;
}
public MyRedisTemplate() {
}
}
MyRedisAutoConfigulation自動配置類
package com.myredis;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
/**
* @author CBeann
* @create 2020-05-30 14:59
*/
@Configuration
@EnableConfigurationProperties(MyRedisProperties.class)
@ConditionalOnProperty(prefix = "myredis",name = "host",
matchIfMissing =false)//如果application.yml或者properties中沒有myredis.host屬性,則此類MyRedisAutoConfigulation不注入IOC容器
public class MyRedisAutoConfigulation {
@Bean
public Jedis jedis(MyRedisProperties myRedisProperties) {
//擷取redis的參數
String host = myRedisProperties.getHost();
String password = myRedisProperties.getPassword();
// 連接配接redis服務
Jedis jedis = new Jedis(host, 6379);
jedis.auth(password);
return jedis;
}
@Bean
public MyRedisTemplate myRedisTemplate(Jedis jedis) {
return new MyRedisTemplate(jedis);
}
}
建立spring.factories
在resources下建立目錄META-INF,在META-INF下建立spring.factories,即resources/META-INF/spring.factories
其中等号(=)左邊為固定值,右邊為自定義的自動配置類
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoCnotallow=\
com.myredis.MyRedisAutoConfigulation
安裝到本地倉庫
clean->install
myredis-spring-boot-starter(Maven項目)
添加上面子產品的依賴
<dependency>
<groupId>com.cbeann</groupId>
<artifactId>myredis-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
安裝到本地倉庫
clean->install
springboot-demo子產品(SpringBoot項目+web)
項目基礎
SpringBoot+web
修改pom檔案
<!--自定義starter-->
<dependency>
<groupId>com.cbeann</groupId>
<artifactId>myredis-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
添加application.yml配置資訊
myredis.host=39.105.30.146
myredis.password=123456
建立controller進行測試
package com.example.springbootdemo.controller;
import com.myredis.MyRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalTime;
/**
* @author CBeann
* @create 2020-05-30 15:25
*/
@RestController
public class HelloController {
@Autowired
private MyRedisTemplate myRedisTemplate;
@RequestMapping("/hello")
public String hello(){
myRedisTemplate.setString("key2", LocalTime.now().toString());
return LocalTime.now().toString();
}
// @RequestMapping("/hello")
// public String hello2(){
// return LocalTime.now().toString();
// }
}
測試結果
http://localhost:8080/hello
redis中添加了key,value資料
總結
1)yml提示
在myredis-spring-boot-autoconfigure中添加依賴,并且類(MyRedisProperties )上有注解
<!--自定義yml提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.2.RELEASE</version>
<optional>true</optional>
</dependency>
參考:自定義starter并在springboot配置檔案中生成提示_枸杞泡茶的部落格
2)如果沒有上圖中的屬性,證明我不用redis,系統不應該報錯
如果我沒有配置myredis的屬性,我也沒用redis,正常的邏輯是不能報錯。
報錯的原因是你沒有配置myredis的資訊,但是他還是加載Jedis并且建立連接配接,那肯定是報錯了,是以不讓他加載MyRedisAutoConfigulation這個類,那麼這個關于自定義Starter的類就全不讓它加載,那麼就和沒有引入這個依賴一樣
如下面代碼所示, 如果application.yml或者properties中沒有myredis.host屬性,則此類MyRedisAutoConfigulation不注入IOC容器
@ConditionalOnProperty(prefix = "myredis",name = "host",
matchIfMissing =false)//如果application.yml或者properties中沒有myredis.host屬性,則此類MyRedisAutoConfigulation不注入IOC容器
public class MyRedisAutoConfigulation {
參考
關與 @EnableConfigurationProperties 注解
關與 @EnableConfigurationProperties 注解 -
自定義starter并在springboot配置檔案中生成提示