SpringBoot封裝自己的Starter的實現(xiàn)方法
一.說明
我們在使用SpringBoot的時候常常要引入一些Starter,例如spring-boot-starter-web,官方為我們提供了幾乎所有的默認配置,很好的降低了使用框架時的復雜度,所以在用xxx-starter的時候,可以不用費心去寫一些繁瑣的配置文件,即使必要的配置在application.properties或application.yml中配置就可以了,當你實現(xiàn)了一個Starter,可以在不同的項目中復用,非常方便,今天我們來編寫自己的Starter以之前的短信業(yè)務為例。
Springboot短信業(yè)務調用: http://www.dbjr.com.cn/article/160092.htm
spring-boot-starter-xxx是官方提供Starter的命名規(guī)則,非官方Starter的命名規(guī)則官方建議為 xxx-spring-boot-starter
二.搭建項目
建立SpringBoot項目,清除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>
<!-- 因為要使用RestTemplate和轉換Json,所以引入這兩個依賴 -->
<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>
二.編寫項目基礎類
創(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;
/**
* 手機號
*/
private String mobile;
/**
* 用戶穿透ID,可以為空
*/
private String uid;
}
創(chuàng)建RestTemplateConfig配置類,用于調用短信接口
/**
* RestTemplateConfig配置
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate( ) {
return new RestTemplate();
}
}
創(chuàng)建短信接口枚舉類,用于存放短信接口API地址
/**
* 短信請求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自動配置類
創(chuàng)建SmsProperties配置屬性類,該類主要用于讀取yml/properties信息
/**
* SMS配置屬性類
* @Author Sans
* @CreateTime 2019/4/20
* @attention 使用ConfigurationProperties注解可將配置文件(yml/properties)中指定前綴的配置轉為bean
*/
@Data
@ConfigurationProperties(prefix = "sms-config")
public class SmsProperties {
private String appid;
private String accountSid;
private String authToken;
}
創(chuàng)建短信核心服務類
/**
* 短信核心服務類
* @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();
}
/**
* 單獨發(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進行訪問遠程Http服務
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進行訪問遠程Http服務
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自動配置類,該類主要用于創(chuàng)建核心業(yè)務類實例
/**
* 短信自動配置類
* @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該文件用來定義需要自動配置的類,SpringBoot啟動時會進行對象的實例化,會通過加載類SpringFactoriesLoader加載該配置文件,將文件中的配置類加載到spring容器
在src/main/resources新建META-INF文件夾,在META-INF文件夾下新建spring.factories文件.配置內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sms.starter.config.SmsAutoConfiguration
五.打包和測試
使用Maven插件,將項目打包安裝到本地倉庫

新建測試項目,引入我們自己的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>
配置測試項目的application.yml
sms-config: account-sid: //這里填寫平臺獲取的ID和KEY auth-token: //這里填寫平臺獲取的ID和KEY appid: //這里填寫平臺獲取的ID和KEY
參數(shù)填寫自己的手機號和申請的模板以及對應的參數(shù)
/**
* 測試短信DEMO
* @Author Sans
* @CreateTime 2019/4/20
* @attention
*/
@RestController
@RequestMapping("/sms")
public class TestController {
@Autowired
private SmsService smsService;
/**
* 短信測試
* @Attention
* @Author: Sans
* @CreateTime: 2019/4/20
*/
@RequestMapping(value = "/sendsmsTest",method = RequestMethod.GET)
public String sendsmsTest(){
//創(chuàng)建傳輸類設置參數(shù)
SendSMSDTO sendSMSDTO = new SendSMSDTO();
sendSMSDTO.setMobile(""); //手機號
sendSMSDTO.setTemplateid(""); //模板
sendSMSDTO.setParam(""); //參數(shù)
return smsService.sendSMS(sendSMSDTO);
}
}
項目源碼: https://gitee.com/liselotte/sms-spring-boot-starter
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java面試LruCache?和?LinkedHashMap及算法實現(xiàn)
這篇文章主要為大家介紹了java面試LruCache?和?LinkedHashMap及算法實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
新建springboot項目時,entityManagerFactory報錯的解決
這篇文章主要介紹了新建springboot項目時,entityManagerFactory報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
JDK1.8使用的垃圾回收器和執(zhí)行GC的時長以及GC的頻率方式
這篇文章主要介紹了JDK1.8使用的垃圾回收器和執(zhí)行GC的時長以及GC的頻率方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringSecurity如何實現(xiàn)配置單個HttpSecurity
這篇文章主要介紹了SpringSecurity如何實現(xiàn)配置單個HttpSecurity,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
HashMap vs TreeMap vs Hashtable vs LinkedHashMap
這篇文章主要介紹了HashMap vs TreeMap vs Hashtable vs LinkedHashMap的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07

