Ape lighthouse has welfare at the end!Copy the code

A. Instructions

When we use SpringBoot, we often need to introduce some Starter, such as spring-boot-starter-web, which officially provides us with almost all the default configuration, which greatly reduces the complexity of using the framework.

So when using XXX-starter, you don’t need to bother to write some complicated configuration files. Even if the necessary configuration is configured in application.properties or application.yml, when you implement a starter, it can be reused in different projects. Very handy, today we’re going to write our own Starter using the previous SMS service as an example.

Spring-boot-starter-xxx indicates the official naming rule for the starter. The unofficial naming rule is recommended as XXx-spring-boot-starter

Ii. Construction project

Set up the SpringBoot project and clear files and folders under Resources

Maven has the following dependencies:

<dependencies> <! > <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - autoconfigure < / artifactId > < version > 2.1.3. RELEASE < / version > < / dependency > <! > <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - configuration - processor < / artifactId > < version > 2.1.3. RELEASE < / version > < / dependency > <! -- Lombok plugin --> <dependency> <groupId>org.projectlombok</groupId> <artifactId> The < version > 1.18.6 < / version > < optional > true < / optional > < / dependency > <! -- Since we're using RestTemplate and converting Json, <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> < version > 2.1.3. RELEASE < / version > < / dependency > < the dependency > < groupId > com. Alibaba < / groupId > The < artifactId > fastjson < / artifactId > < version > 1.2.45 < / version > < / dependency > < / dependencies >Copy the code

Spring-boot-configuration-processor is not required. It generates spring-configuration-metadata.json during compilation. This file is used for IDEA.

In application. Yml, you can use Ctrl+ left mouse button to click on the property name. IDE will jump to the class where you configured the property and write application.

3. Write the project foundation class

Create the SendSMSDTO transport class for parameter passing

/** * SMSDTO argument class *@Author Sans
 * @CreateTime 2019/4/20 
 * @attention* /
@Data
public class SendSMSDTO {
    /** * template ID */
    private String templateid;
    /** * parameter */
    private String param;
    /** * Mobile phone number */
    private String mobile;
    /** * User penetration ID, can be empty */
    private String uid;
}Copy the code

Create the RestTemplateConfig configuration class to invoke the SMS interface

/** * RestTemplateConfig configuration *@Author Sans
 * @CreateTime 2019/4/20 
 * @attention* /
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate( ) {
        return newRestTemplate(); }}Copy the code

Create an SMS interface enumeration class to store the SMS API addresses

/** * SMS request API enumeration *@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; }}Copy the code

Write the Starter auto-configuration class

Create the SmsProperties configuration property class, which is used to read YML/Properties information

/** * SMS config attribute class *@Author Sans
 * @CreateTime 2019/4/20 
 * @attentionUse the ConfigurationProperties annotation to convert the configuration of the specified prefix in the configuration file (YML/Properties) to bean */
@Data
@ConfigurationProperties(prefix = "sms-config")
public class SmsProperties {
    private String appid;
    private String accountSid;
    private String authToken;
}Copy the code

Create a core SMS service class

/** * SMS core services *@Author Sans
 * @CreateTime 2019/4/20 
 * @attention* /
public class SmsService {

    @Autowired
    private RestTemplate restTemplate;
    private String appid;
    private String accountSid;
    private String authToken;

    /** * initializes */
    public SmsService(SmsProperties smsProperties) {
       this.appid = smsProperties.getAppid();
       this.accountSid = smsProperties.getAccountSid();
       this.authToken = smsProperties.getAuthToken();
    }

    /** ** send */ separately
    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);
        // Use restTemplate to access the remote Http service
        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;
    }

    /** * group sends */
    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);
        // Use restTemplate to access the remote Http service
        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);
        returnresult; }}Copy the code

Create the SmsAutoConfiguration automatic configuration class, which is used to create instances of core business classes

/** * SMS automatic configuration class *@Author Sans
 * @CreateTime 2019/4/20 
 * @attention* /
@Configuration  
@EnableConfigurationProperties(SmsProperties.class)// Enable the @configurationProperties annotation
public class SmsAutoConfiguration {
    @Bean
    public SmsService getBean(SmsProperties smsProperties){
        SmsService smsService = new SmsService(smsProperties);
        returnsmsService; }}Copy the code

Create a Spring. factories file

The spring.factories file is used to define the classes that need to be configured automatically. When SpringBoot starts, the object is instantiated and the configuration file is loaded by loading the SpringFactoriesLoader class. Load the configuration classes in the file into the Spring container. Create a meta-INF folder in SRC /main/resources and a Spring. factories file in the meta-INF folder. The configuration is as follows:

 org.springframework.boot.autoconfigure.EnableAutoConfiguration=
        com.sms.starter.config.SmsAutoConfigurationCopy the code

Package and test

Using the Maven plug-in, package the project and install it into a local repository

To create a new test project and introduce our own Starter, Maven relies on the following:

<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 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code

Configure the application. Yml of the test project

sms-config:
  account-sid:  // Enter the ID and KEY obtained by the platform
  auth-token:   // Enter the ID and KEY obtained by the platform
  appid:        // Enter the ID and KEY obtained by the platformCopy the code

Parameter Enter your mobile phone number, application template, and corresponding parameters

/** * Test SMS DEMO *@Author Sans
 * @CreateTime 2019/4/20 
 * @attention* /
@RestController
@RequestMapping("/sms")
public class TestController {
    @Autowired
    private SmsService smsService;
    /** * SMS test *@Attention
     * @Author: Sans
     * @CreateTime: 2019/4/20 * /
    @RequestMapping(value = "/sendsmsTest",method = RequestMethod.GET)
    public String sendsmsTest(a){
        // Create transport class Settings parameters
        SendSMSDTO sendSMSDTO  = new SendSMSDTO();
        sendSMSDTO.setMobile("");     / / cell phone number
        sendSMSDTO.setTemplateid(""); / / template
        sendSMSDTO.setParam("");      / / parameters
        returnsmsService.sendSMS(sendSMSDTO); }}Copy the code

Wechat search BGM7756, free access to a full set of architecture information!



Space is limited! Some information pictures are as follows!