SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法
一.說明
我們?cè)谑褂肧pringBoot的時(shí)候常常要引入一些Starter,例如spring-boot-starter-web,官方為我們提供了幾乎所有的默認(rèn)配置,很好的降低了使用框架時(shí)的復(fù)雜度,所以在用xxx-starter的時(shí)候,可以不用費(fèi)心去寫一些繁瑣的配置文件,即使必要的配置在application.properties或application.yml中配置就可以了,當(dāng)你實(shí)現(xiàn)了一個(gè)Starter,可以在不同的項(xiàng)目中復(fù)用,非常方便,今天我們來編寫自己的Starter以之前的短信業(yè)務(wù)為例。
Springboot短信業(yè)務(wù)調(diào)用: http://www.dhdzp.com/article/160092.htm
spring-boot-starter-xxx是官方提供Starter的命名規(guī)則,非官方Starter的命名規(guī)則官方建議為 xxx-spring-boot-starter
二.搭建項(xiàng)目
建立SpringBoot項(xiàng)目,清除resources下的文件和文件夾

Maven依賴如下:
<dependencies>
<!--封裝Starter核心依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- lombok 插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<optional>true</optional>
</dependency>
<!-- 因?yàn)橐褂肦estTemplate和轉(zhuǎn)換Json,所以引入這兩個(gè)依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
</dependency>
</dependencies>
二.編寫項(xiàng)目基礎(chǔ)類
創(chuàng)建SendSMSDTO傳輸類,用于參數(shù)傳遞
/**
* SMSDTO參數(shù)類
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@Data
public class SendSMSDTO {
/**
* 模板ID
*/
private String templateid;
/**
* 參數(shù)
*/
private String param;
/**
* 手機(jī)號(hào)
*/
private String mobile;
/**
* 用戶穿透ID,可以為空
*/
private String uid;
}
創(chuàng)建RestTemplateConfig配置類,用于調(diào)用短信接口
/**
* RestTemplateConfig配置
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate( ) {
return new RestTemplate();
}
}
創(chuàng)建短信接口枚舉類,用于存放短信接口API地址
/**
* 短信請(qǐng)求API枚舉
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@Getter
public enum ENUM_SMSAPI_URL {
SENDSMS("https://open.ucpaas.com/ol/sms/sendsms"),
SENDBATCHSMS("https://open.ucpaas.com/ol/sms/sendsms_batch");
private String url;
ENUM_SMSAPI_URL(String url) {
this.url = url;
}
}
三.編寫Starter自動(dòng)配置類
創(chuàng)建SmsProperties配置屬性類,該類主要用于讀取yml/properties信息
/**
* SMS配置屬性類
* @Author Sans
* @CreateTime 2019/4/20
* @attention 使用ConfigurationProperties注解可將配置文件(yml/properties)中指定前綴的配置轉(zhuǎn)為bean
*/
@Data
@ConfigurationProperties(prefix = "sms-config")
public class SmsProperties {
private String appid;
private String accountSid;
private String authToken;
}
創(chuàng)建短信核心服務(wù)類
/**
* 短信核心服務(wù)類
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
public class SmsService {
@Autowired
private RestTemplate restTemplate;
private String appid;
private String accountSid;
private String authToken;
/**
* 初始化
*/
public SmsService(SmsProperties smsProperties) {
this.appid = smsProperties.getAppid();
this.accountSid = smsProperties.getAccountSid();
this.authToken = smsProperties.getAuthToken();
}
/**
* 單獨(dú)發(fā)送
*/
public String sendSMS(SendSMSDTO sendSMSDTO){
JSONObject jsonObject = new JSONObject();
jsonObject.put("sid", accountSid);
jsonObject.put("token", authToken);
jsonObject.put("appid", appid);
jsonObject.put("templateid", sendSMSDTO.getTemplateid());
jsonObject.put("param", sendSMSDTO.getParam());
jsonObject.put("mobile", sendSMSDTO.getMobile());
if (sendSMSDTO.getUid()!=null){
jsonObject.put("uid",sendSMSDTO.getUid());
}else {
jsonObject.put("uid","");
}
String json = JSONObject.toJSONString(jsonObject);
//使用restTemplate進(jìn)行訪問遠(yuǎn)程Http服務(wù)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<String>(json, headers);
String result = restTemplate.postForObject(ENUM_SMSAPI_URL.SENDSMS.getUrl(), httpEntity, String.class);
return result;
}
/**
* 群體發(fā)送
*/
public String sendBatchSMS(SendSMSDTO sendSMSDTO){
JSONObject jsonObject = new JSONObject();
jsonObject.put("sid", accountSid);
jsonObject.put("token", authToken);
jsonObject.put("appid", appid);
jsonObject.put("templateid", sendSMSDTO.getTemplateid());
jsonObject.put("param", sendSMSDTO.getParam());
jsonObject.put("mobile", sendSMSDTO.getMobile());
if (sendSMSDTO.getUid()!=null){
jsonObject.put("uid",sendSMSDTO.getUid());
}else {
jsonObject.put("uid","");
}
String json = JSONObject.toJSONString(jsonObject);
//使用restTemplate進(jìn)行訪問遠(yuǎn)程Http服務(wù)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<String>(json, headers);
String result = restTemplate.postForObject(ENUM_SMSAPI_URL.SENDBATCHSMS.getUrl(), httpEntity, String.class);
return result;
}
}
創(chuàng)建SmsAutoConfiguration自動(dòng)配置類,該類主要用于創(chuàng)建核心業(yè)務(wù)類實(shí)例
/**
* 短信自動(dòng)配置類
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@Configuration
@EnableConfigurationProperties(SmsProperties.class)//使@ConfigurationProperties注解生效
public class SmsAutoConfiguration {
@Bean
public SmsService getBean(SmsProperties smsProperties){
SmsService smsService = new SmsService(smsProperties);
return smsService;
}
}
四.創(chuàng)建spring.factories文件
spring.factories該文件用來定義需要自動(dòng)配置的類,SpringBoot啟動(dòng)時(shí)會(huì)進(jìn)行對(duì)象的實(shí)例化,會(huì)通過加載類SpringFactoriesLoader加載該配置文件,將文件中的配置類加載到spring容器
在src/main/resources新建META-INF文件夾,在META-INF文件夾下新建spring.factories文件.配置內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sms.starter.config.SmsAutoConfiguration
五.打包和測(cè)試
使用Maven插件,將項(xiàng)目打包安裝到本地倉(cāng)庫(kù)

新建測(cè)試項(xiàng)目,引入我們自己的Starter,Maven依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加我們自己的starter-->
<dependency>
<groupId>com.sms.starter</groupId>
<artifactId>sms-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
配置測(cè)試項(xiàng)目的application.yml
sms-config: account-sid: //這里填寫平臺(tái)獲取的ID和KEY auth-token: //這里填寫平臺(tái)獲取的ID和KEY appid: //這里填寫平臺(tái)獲取的ID和KEY
參數(shù)填寫自己的手機(jī)號(hào)和申請(qǐng)的模板以及對(duì)應(yīng)的參數(shù)
/**
* 測(cè)試短信DEMO
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@RestController
@RequestMapping("/sms")
public class TestController {
@Autowired
private SmsService smsService;
/**
* 短信測(cè)試
* @Attention
* @Author: Sans
* @CreateTime: 2019/4/20
*/
@RequestMapping(value = "/sendsmsTest",method = RequestMethod.GET)
public String sendsmsTest(){
//創(chuàng)建傳輸類設(shè)置參數(shù)
SendSMSDTO sendSMSDTO = new SendSMSDTO();
sendSMSDTO.setMobile(""); //手機(jī)號(hào)
sendSMSDTO.setTemplateid(""); //模板
sendSMSDTO.setParam(""); //參數(shù)
return smsService.sendSMS(sendSMSDTO);
}
}
項(xiàng)目源碼: https://gitee.com/liselotte/sms-spring-boot-starter
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合quartz定時(shí)任務(wù)框架的完整步驟
在做項(xiàng)目時(shí)有時(shí)候會(huì)有定時(shí)器任務(wù)的功能,比如某某時(shí)間應(yīng)該做什么,多少秒應(yīng)該怎么樣之類的,下面這篇文章主要給大家介紹了關(guān)于springboot整合quartz定時(shí)任務(wù)框架的相關(guān)資料,需要的朋友可以參考下2022-01-01
JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring系列中的beanFactory與ApplicationContext
這篇文章主要介紹了Spring系列中的beanFactory與ApplicationContext,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Json轉(zhuǎn)list二層解析轉(zhuǎn)換代碼實(shí)例
這篇文章主要介紹了Json轉(zhuǎn)list二層解析轉(zhuǎn)換代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
java操作elasticsearch詳細(xì)方法總結(jié)
elasticsearch是使用Java編寫的一種開源搜索引擎,也是一種分布式的搜索引擎架構(gòu),這篇文章主要給大家介紹了關(guān)于java操作elasticsearch的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
SpringBoot結(jié)合Tess4J實(shí)現(xiàn)拍圖識(shí)字的示例代碼
圖片中的文字提取已經(jīng)越來越多地應(yīng)用于數(shù)據(jù)輸入和自動(dòng)化處理過程,本文主要介紹了SpringBoot結(jié)合Tess4J實(shí)現(xiàn)拍圖識(shí)字的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計(jì)算機(jī)應(yīng)用環(huán)境下的自動(dòng)化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下2018-08-08
idea?Maven?插件?docker-maven-plugin?打包docker鏡像上傳到遠(yuǎn)程倉(cāng)庫(kù)的過程詳解
這篇文章主要介紹了idea Maven插件docker-maven-plugin打包docker鏡像上傳到遠(yuǎn)程倉(cāng)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

