- 
认真是一种态度,坚持是一种品格,优秀是一种习惯!
 最近被因为一场疫情,被关家了。2020愿一切早点好起来。
 在SpringBoot的使用过程中,我们深刻体会到了Springboot starter带来的方便快捷,通过maven构建的项目,只需要在pom.xml中引入某个starter,我们不用写一行代码,只需简单的加些配置,一个基础项目就有了。那我们是否也可以自己搞个自己的SDK,当然可以。
 尝试自己写个简单的DEMO:
基本步骤:
- 创建SpringBoot项目,引入自动配置依赖:
- 添加配置文件类(xxxProperties)和自动配置类
- 封装SDK业务实现
- 在resources目录下增加META-INF/spring.factpries配置自动配置类
- 构建SDK包,maven-install发布到本地仓库测试
- 若有条件发布到自有私库提供给其他项目使用
1.首先创建自己的SpringBoot Starter项目:直接创建一个Maven项目(注意项目名称:自定义名称-spring-boot-starter):如下图

2.引入自动配置相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acm</groupId>
    <artifactId>acm-sdk-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
3.创建配置属性类和自动装配Configuration
项目基本结构:

4.编写对应的实现代码
- 属性文件,假设我准备支持阿里云和sendCloud的短信API发送
package com.acm.sdk.properties;
import com.acm.sdk.constant.SmsTypes;
import com.acm.sdk.properties.type.AliYun;
import com.acm.sdk.properties.type.SendCloud;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.io.Serializable;
/**
 * <p>
 * SmsProperties
 * </p>
 *
 * @author jackcooperz
 * @date 2020/3/11 9:02
 */
@Data
@ConfigurationProperties(prefix = SmsProperties.SMS_NAME_PREFIX)
public class SmsProperties implements Serializable {
    /**
     * 短信配置前缀
     */
    public static final String SMS_NAME_PREFIX = "sms";
    /**
     * 短信API类型 默认短信API类型 默认SendCloud
     */
    private String defaultSmsType = SmsTypes.SEND_CLOUD.getSmsType();
    /**
     * SendCloud 短信API
     */
    private SendCloud sendCloud;
    /**
     * 阿里云 短信API
     */
    private AliYun aliyun;
}
- 业务实现Service(仅做demo)
package com.acm.sdk.service;
import com.acm.sdk.constant.SmsTypes;
import com.acm.sdk.properties.SmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * <p>
 * SmsService
 * </p>
 *
 * @author jackcooperz
 * @date 2020/3/11 9:08
 */
@Service
public class SmsService {
    @Autowired
    private SmsProperties smsProperties;
    /**
     * 读取配置文件显示返回测试
     *
     * @return SmsProperties
     */
    public SmsProperties readSmsProperties() {
        return smsProperties;
    }
    /**
     * 发送短信
     *
     * @return String
     */
    public String sendSms() {
        if (SmsTypes.ALI_YUN.getSmsType().equals(smsProperties.getDefaultSmsType())) {
            return "阿里云短信发送成功";
        } else if (SmsTypes.SEND_CLOUD.getSmsType().equals(smsProperties.getDefaultSmsType())) {
            return "sendCloud短信发送成功";
        }
        return "未知的短信API类型,暂不支持";
    }
}
- 编写自动装配类
package com.acm.sdk.configuration;
import com.acm.sdk.properties.SmsProperties;
import com.acm.sdk.service.SmsService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
 * <p>
 * SmsAutoConfiguration
 * </p>
 *
 * @author jackcooperz
 * @date 2020/3/11 9:05
 */
@Configuration
@ComponentScan("com.acm.sdk")
@EnableConfigurationProperties(value = {SmsProperties.class})
@ConditionalOnClass(SmsService.class)
public class SmsAutoConfiguration {
}
- 添加Spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.acm.sdk.configuration.SmsAutoConfiguration
- maven-install构建SDK,
 构建SDK
  
编写DEMO测试看看
引入SDK
 <dependency>
            <groupId>com.acm</groupId>
            <artifactId>acm-sdk-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
配置属性
sms:
  default-sms-type: aliyun
  aliyun:
   signName: test
   accessKeyId: test
   accessKeySecret: test
编写接口测试看看
   @Autowired
    private SmsService smsService;
    @ResponseBody
    @RequestMapping(value = "/readSms")
    public SmsProperties readSms() {
        return smsService.readSmsProperties();
    }
 
    @ResponseBody
    @RequestMapping(value = "/send")
    public String sendSms() {
        return smsService.sendSms();
    }


至此一个自己的spring-boot-starter就基本完成了,剩下的就是根据自己的业务封装SDK。
附上项目DEMO地址:https://gitee.com/jingchu/acm-sdk-spring-boot-starter.git









