springboot2.x基礎教程:動手製作一個starter包
阿新 • • 發佈:2020-09-12
> 上一篇部落格介紹了springboot自動裝配的原理。springboot本身有豐富的spring-boot-starter-xx整合元件,這一篇趁熱打鐵加深理解,我們利用springboot自動裝配的機制,從零開始製作一個屬於自己的starter包。
## 製作一個starter包思路
這一篇部落格我製作一個上傳圖片第三方圖床的starter,整合常見的第三方圖床sm.ms、imgur、github圖床等。
本教程不會具體的講解圖床上傳相關的程式碼,而是主要分析封裝此starter的思路。
1. 首先安裝springboot第三方的starter規範命名:xx-spring-boot-starter,我們專案取名為imghost-spring-boot-starter。
2. 對於圖床相關的配置項,我們同樣準備建立一個ImgHostProperties配置類存放。
3. 同樣我們也需要一個ImgHostAutoConfiguration,並且加上條件註解在某些情況下才會注入我們的工具類到IOC容器中。
4. 按照規範在我們專案的META-INF/spring.factories檔案下,指定我們starter的自動裝配類。
## 專案結構一覽
![image-20200911224040555](https://i.loli.net/2020/09/11/evtQ71Jr3cpgEiK.png)
## Starter開發例項
### 引入必要的依賴
這裡主要引入spring-boot-starter包,spring-boot-configuration-processor其他依賴主要為上傳到第三方圖床傳送Http請求依賴包。
```xml
4.0.0
vip.codehome
imghost-spring-boot-starter
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
org.springframework.boot
spring-boot-starter
org.projectlombok
lombok
1.18.12
compile
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-json
com.squareup.okhttp3
okhttp
3.14.9
src/main/java
**/spring.factories
false
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.8
org.apache.maven.plugins
maven-source-plugin
3.0.1
compile
jar-no-fork
```
### 定義一個圖床引數上傳的配置類
上傳到SM.MS的API需要上傳的token,在sm.ms網站註冊獲取個人的私鑰,後面如果上傳到imgur同樣可以在此類中加入對應的配置類。
```java
@Data
@ConfigurationProperties(prefix = "imghost")
public class ImgHostProperties {
SMMS smms;
@Data
public static class SMMS{
String token;
}
}
```
### 定義上傳服務AutoConfiguration類
當imghost.smms.token使用者配置時,我們生成一個SMMSImgHostService的圖床上傳服務類。
```java
@Configuration
@EnableConfigurationProperties(ImgHostProperties.class)
@ConditionalOnProperty(prefix = "imghost",name = "enabled",havingValue = "true",matchIfMissing = true)
public class ImgHostAutoConfiguration {
private final ImgHostProperties imgHostProperties;
public ImgHostAutoConfiguration(ImgHostProperties imgHostProperties) {
this.imgHostProperties = imgHostProperties;
}
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix="imghost.smms",name="token")
@Bean
public SMMSImgHostService imgHostService() {
return new SMMSImgHostService(imgHostProperties);
}
}
```
### 編寫spring.factories
最後在專案的src/main/resource上加入META-INF/spring.factories中引入我們自定義的ImgHostAutoConfiguration配置類。
```
org.springframework.boot.autoconfigure.EnableAutoConfiguration=vip.codehome.imghost.ImgHostAutoConfiguration
```
### 如何使用
1. 在使用的專案中引入我們的imghost-spring-boot-starter。
```
vip.codehome
imghost-spring-boot-starter
1.0-SNAPSHOT
```
2. 在springboot專案中加入如下配置
![image-20200911224308917](https://i.loli.net/2020/09/11/sryCpbKwqYm2PlZ.png)
3. 專案使用
```java
@Autowired
SMMSImgHostService smms;
public void upload() {
System.out.println(smms.upload(newFile("D:\\test.jpg")));
}
```
## 總結
**千里之行,始於足下。這裡是SpringBoot教程系列第十八篇。以上就是我們自己動手製作一個starter包的全過程,是不是很簡單。此專案在[github可下載原始碼](https://github.com/mytianya/imghost-spring-boot-starter)**
**當前只是實現了上傳到SM.MS圖床,後期會逐漸迭代一個上傳到sm.ms,imgur,github各種圖床的通用工具類,敬請期待。如果覺得不錯,點贊、評論、關注三