栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot封装自己的Starter的实现方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot封装自己的Starter的实现方法

一.说明

我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot-starter-web,官方为我们提供了几乎所有的默认配置,很好的降低了使用框架时的复杂度,所以在用xxx-starter的时候,可以不用费心去写一些繁琐的配置文件,即使必要的配置在application.properties或application.yml中配置就可以了,当你实现了一个Starter,可以在不同的项目中复用,非常方便,今天我们来编写自己的Starter以之前的短信业务为例。

Springboot短信业务调用: https://www.jb51.net/article/160092.htm

spring-boot-starter-xxx是官方提供Starter的命名规则,非官方Starter的命名规则官方建议为 xxx-spring-boot-starter

二.搭建项目

建立SpringBoot项目,清除resources下的文件和文件夹

Maven依赖如下:


    
    
      org.springframework.boot
      spring-boot-starter
      2.1.3.RELEASE
    
    
      org.springframework.boot
      spring-boot-autoconfigure
      2.1.3.RELEASE
    
    
      org.springframework.boot
      spring-boot-configuration-processor
      2.1.3.RELEASE
    
    
    
      org.projectlombok
      lombok
      1.18.6
      true
    
    
    
      org.springframework.boot
      spring-boot-starter-web
      2.1.3.RELEASE
    
    
      com.alibaba
      fastjson
      1.2.45
    
  

二.编写项目基础类

创建SendSMSDTO传输类,用于参数传递


@Data
public class SendSMSDTO {
  
  private String templateid;
  
  private String param;
  
  private String mobile;
  
  private String uid;
}

创建RestTemplateConfig配置类,用于调用短信接口


@Configuration
public class RestTemplateConfig {
  @Bean
  public RestTemplate restTemplate( ) {
    return new RestTemplate();
  }
}

创建短信接口枚举类,用于存放短信接口API地址


@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自动配置类

创建SmsProperties配置属性类,该类主要用于读取yml/properties信息


@Data
@ConfigurationProperties(prefix = "sms-config")
public class SmsProperties {
  private String appid;
  private String accountSid;
  private String authToken;
}

创建短信核心服务类


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();
  }

  
  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 httpEntity = new HttpEntity(json, headers);
    String result = restTemplate.postForObject(ENUM_SMSAPI_URL.SENDSMS.getUrl(), httpEntity, String.class);
    return result;
  }

  
  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 httpEntity = new HttpEntity(json, headers);
    String result = restTemplate.postForObject(ENUM_SMSAPI_URL.SENDBATCHSMS.getUrl(), httpEntity, String.class);
    return result;
  }
}

创建SmsAutoConfiguration自动配置类,该类主要用于创建核心业务类实例


@Configuration 
@EnableConfigurationProperties(SmsProperties.class)//使@ConfigurationProperties注解生效
public class SmsAutoConfiguration {
  @Bean
  public SmsService getBean(SmsProperties smsProperties){
    SmsService smsService = new SmsService(smsProperties);
    return smsService;
  }
}

四.创建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依赖如下:


    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      com.sms.starter
      sms-spring-boot-starter
      0.0.1-SNAPSHOT
    

配置测试项目的application.yml

sms-config:
 account-sid: //这里填写平台获取的ID和KEY
 auth-token:  //这里填写平台获取的ID和KEY
 appid:    //这里填写平台获取的ID和KEY

参数填写自己的手机号和申请的模板以及对应的参数


@RestController
@RequestMapping("/sms")
public class TestController {
  @Autowired
  private SmsService smsService;
  
  @RequestMapping(value = "/sendsmsTest",method = RequestMethod.GET)
  public String sendsmsTest(){
    //创建传输类设置参数
    SendSMSDTO sendSMSDTO = new SendSMSDTO();
    sendSMSDTO.setMobile("");   //手机号
    sendSMSDTO.setTemplateid(""); //模板
    sendSMSDTO.setParam("");   //参数
    return smsService.sendSMS(sendSMSDTO);
  }
}

项目源码: https://gitee.com/liselotte/sms-spring-boot-starter

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138372.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号