SpringBoot

坚持学习第六天:SpringBoot 2.x 编写自己的Spri

2020-03-11  本文已影响0人  醉枫浅墨

基本步骤:

1.首先创建自己的SpringBoot Starter项目:直接创建一个Maven项目(注意项目名称:自定义名称-spring-boot-starter):如下图
image.png
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.编写对应的实现代码
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;
}
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 {
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.acm.sdk.configuration.SmsAutoConfiguration
编写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
上一篇 下一篇

猜你喜欢

热点阅读