SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法
一.說(shuō)明
我們?cè)谑褂肧pringBoot的時(shí)候常常要引入一些Starter,例如spring-boot-starter-web,官方為我們提供了幾乎所有的默認(rèn)配置,很好的降低了使用框架時(shí)的復(fù)雜度,所以在用xxx-starter的時(shí)候,可以不用費(fèi)心去寫(xiě)一些繁瑣的配置文件,即使必要的配置在application.properties或application.yml中配置就可以了,當(dāng)你實(shí)現(xiàn)了一個(gè)Starter,可以在不同的項(xiàng)目中復(fù)用,非常方便,今天我們來(lái)編寫(xiě)自己的Starter以之前的短信業(yè)務(wù)為例。
Springboot短信業(yè)務(wù)調(diào)用: http://www.dbjr.com.cn/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ě)項(xiàng)目基礎(chǔ)類(lèi)
創(chuàng)建SendSMSDTO傳輸類(lèi),用于參數(shù)傳遞
/** * SMSDTO參數(shù)類(lèi) * @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配置類(lèi),用于調(diào)用短信接口
/** * RestTemplateConfig配置 * @Author Sans * @CreateTime 2019/4/20 * @attention */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate( ) { return new RestTemplate(); } }
創(chuàng)建短信接口枚舉類(lèi),用于存放短信接口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; } }
三.編寫(xiě)Starter自動(dòng)配置類(lèi)
創(chuàng)建SmsProperties配置屬性類(lèi),該類(lèi)主要用于讀取yml/properties信息
/** * SMS配置屬性類(lèi) * @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ù)類(lèi)
/** * 短信核心服務(wù)類(lèi) * @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)行訪問(wè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)行訪問(wè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)配置類(lèi),該類(lèi)主要用于創(chuàng)建核心業(yè)務(wù)類(lèi)實(shí)例
/** * 短信自動(dòng)配置類(lèi) * @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該文件用來(lái)定義需要自動(dòng)配置的類(lèi),SpringBoot啟動(dòng)時(shí)會(huì)進(jìn)行對(duì)象的實(shí)例化,會(huì)通過(guò)加載類(lèi)SpringFactoriesLoader加載該配置文件,將文件中的配置類(lèi)加載到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: //這里填寫(xiě)平臺(tái)獲取的ID和KEY auth-token: //這里填寫(xiě)平臺(tái)獲取的ID和KEY appid: //這里填寫(xiě)平臺(tái)獲取的ID和KEY
參數(shù)填寫(xiě)自己的手機(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)建傳輸類(lèi)設(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)文章
java面試LruCache?和?LinkedHashMap及算法實(shí)現(xiàn)
這篇文章主要為大家介紹了java面試LruCache?和?LinkedHashMap及算法實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決
這篇文章主要介紹了新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01JDK1.8使用的垃圾回收器和執(zhí)行GC的時(shí)長(zhǎng)以及GC的頻率方式
這篇文章主要介紹了JDK1.8使用的垃圾回收器和執(zhí)行GC的時(shí)長(zhǎng)以及GC的頻率方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Java數(shù)據(jù)結(jié)構(gòu)之鏈表的概念及結(jié)構(gòu)
這篇文章主要介紹了數(shù)據(jù)鏈表的概念及結(jié)構(gòu),鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的。想進(jìn)一步了解的同學(xué),可以參考閱讀本文2023-04-04SpringSecurity如何實(shí)現(xiàn)配置單個(gè)HttpSecurity
這篇文章主要介紹了SpringSecurity如何實(shí)現(xiàn)配置單個(gè)HttpSecurity,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08HashMap vs TreeMap vs Hashtable vs LinkedHashMap
這篇文章主要介紹了HashMap vs TreeMap vs Hashtable vs LinkedHashMap的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Java各種比較對(duì)象的方式的對(duì)比總結(jié)
比較對(duì)象是面向?qū)ο缶幊陶Z(yǔ)言的一個(gè)基本特征.在本教程中,我們將介紹Java語(yǔ)言的一些特性,這些特性允許我們比較對(duì)象.此外,我們還將研究外部庫(kù)中的這些特性,需要的朋友可以參考下2021-06-06