就自己来实现一个SpringBoot的 starter吧。废话不多说我们还是直入主题吧。
在这里插入图片描述
什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧。

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

纳尼,一大堆的英文,这还有兴趣接着往下看吗?是不是看到这直接退出了。都到门口了,不进来喝杯茶再走嘛?看都看到这了还是接着继续往下看吧。我们先不解释这一段话是什么意思,我们可以看看starter的出现给我们解决了什么问题。
我们还是以上述官网的例子来进行说明比如说我们需要在Spring 中适应JPA来操作数据库。
在没有springBoot-starter之前,我们需要引入jpa的步骤

通过maven 引入jdbc的依赖、以及jpa相关的各种依赖
编写jpa相关的配置文件
网上各种查询找资料进行调试,调试的过程对于新手可能会有点奔溃会遇到各种奇奇怪怪的问题,jar包冲突啊,这个jar包下载不下来,缺少某个jar包。
终于在经历千辛万苦,哼次哼次的解决各种问题之后终于把项目跑起来了,然后把这次整合jpa遇到的问题,以及整合的步骤都一一的详细记录下来。方便下次在需要整合jpa的时候直接copy就好了。
我们以前在没有starter之前是不是都是这么玩的。这样的缺点是不是也非常显著,比如过程复杂、需要不停的粘贴复制(不过这是程序员经常干的事情了,也不在乎多一两次了)、整合其它组件到自己的项目变的困难,效率低下。这也就造成了996的程序员比较多了(晚上就不能够回去69了)。
在这里插入图片描述
SpringBoot Starter的出现
我们可以看下SpringBoot 现在都为我们提供有哪些starter,我这边这截图了部分starter,更多的请点击https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters
在这里插入图片描述
starter的实现:虽然我们每个组件的starter实现各有差异,但是它们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfiguration。因为Spring Boot提倡“约定大于配置”这一理念,所以我们使用ConfigurationProperties来保存我们的配置,并且这些配置都可以有一个默认值,即在我们没有主动覆写原始配置的情况下,默认值就会生效。除此之外,starter的ConfigurationProperties还使得所有的配置属性被聚集到一个文件中(一般在resources目录下的application.properties),这样我们就告别了Spring项目中XML地狱。
starter的出现帮把我们把各种复杂的配置都封装起来了,让我们真正的可以达到了开箱即用。不仅降低了我们使用它的门槛,并且还大大提高了我们的开发效率。正如前面所说《SpringBoot自动装配》让我们有更多的时间去陪女朋友。

实现自己的SpringBoot Starter
命名规范
如果你快有孩子了,出生前你比较急的一定是起个名字。孩子的姓名标识着你和你爱人的血统,一定不会起隔壁老王的姓氏,肯定会招来异样的眼光。在maven中,groupId代表着姓氏,artifactId代表着名字。Spring Boot也是有一个命名的建议的。所以名字是不能够随随便便取得,可以按照官方的建议来取。

What’s in a name
All official starters follow a similar naming pattern; spring-boot-starter-*, where * is a particular type of application. This naming structure is intended to help when you need to find a starter. The Maven integration in many IDEs lets you search dependencies by name. For example, with the appropriate Eclipse or STS plugin installed, you can press ctrl-space in the POM editor and type “spring-boot-starter” for a complete list.
As explained in the “Creating Your Own Starter” section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts. Rather, a third-party starter typically starts with the name of the project. For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.

大概意思是
官方的 starter 的命名格式为 spring-boot-starter-{xxxx} 比如spring-boot-starter-activemq
第三方我们自己的命名格式为 {xxxx}-spring-boot-starter。比如mybatis-spring-boot-starter。
如果我们忽略这种约定,是不是会显得我们写的东西不够“专业“。

自定义一个Starter
下面我们就来实现一个自定义的发送短信的starter,命名为sms-spring-boot-starter。

引入pom

org.springframework.boot
spring-boot-starter

org.springframework.boot
spring-boot-configuration-processor
true

org.projectlombok
lombok
1.16.18
provided

编写配置文件
发短信我们需要配置一些账号信息,不同的短信供应商,账户信息是不一样的,所以我们需要定义一个XXXXProperties 来自动装配这些账户信息。下面我们就以腾讯云和阿里云两家供应商为例;

@ConfigurationProperties(prefix = “sms”)
@Data
public class SmsProperties {

private SmsMessage aliyun = new SmsMessage();private SmsMessage tencent = new SmsMessage();@Data
public static class SmsMessage{/*** 用户名*/private String userName;/*** 密码*/private String passWord;/*** 秘钥*/private String sign;/****/private String url;@Overridepublic String toString() {return "SmsMessage{" +"userName='" + userName + '\'' +", passWord='" + passWord + '\'' +", sign='" + sign + '\'' +", url='" + url + '\'' +'}';}
}

}
如果需要在其他项目中使用发送短信功能的话,我们只需要在配置文件(application.yml)中配置SmsProperties 的属性信息就可以了。 比如:

sms:
aliyun:
pass-word: 12345
user-name: java金融
sign: 阿里云
url: http://aliyun.com/send
tencent:
pass-word: 6666
user-name: java金融
sign: 腾讯云
url: http://tencent.com/send
还记的@ConfigurationProperties注解里面是不是有个prefix 属性,我们配置的这个属性是sms,配置这个的主要一个作用的话是主要用来区别各个组件的参数。这里有个小知识点需要注意下当我们在配置文件输入sms我们的idea会提示这个sms有哪些属性可以配置,以及每个属性的注释都有标记,建议的话注释还是写英文,这样会显得你比较专业。
在这里插入图片描述
这个提示的话,是需要引入下面这个jar的。

org.springframework.boot spring-boot-configuration-processor true 引入这个jar之后,我们编译之后就会在META-INF文件夹下面生成一个spring-configuration-metadata.json的文件。 在这里插入图片描述 我们可以看到这个文件其实 是根据SmsProperties类的成员属性来生成的。

然后在编写短信自动配置类:
@EnableConfigurationProperties(value = SmsProperties.class)
@Configuration
public class SmsAutoConfiguration {
/**
* 阿里云发送短信的实现类
* @param smsProperties
* @return
/
@Bean
public AliyunSmsSenderImpl aliYunSmsSender(SmsProperties smsProperties){
return new AliyunSmsSenderImpl(smsProperties.getAliyun());
}
/
*
* 腾讯云发送短信的实现类
* @param smsProperties
* @return
*/
@Bean
public TencentSmsSenderImpl tencentSmsSender(SmsProperties smsProperties){
return new TencentSmsSenderImpl(smsProperties.getTencent());
}
}
编写我们的发送短信实现类:

public class AliyunSmsSenderImpl implements SmsSender {

private SmsMessage smsMessage;public AliyunSmsSenderImpl(SmsMessage smsProperties) {this.smsMessage = smsProperties;
}@Override
public boolean send(String message) {System.out.println(smsMessage.toString()+"开始发送短信==》短信内容:"+message);return true;
}

}
让starter生效
starter集成应用有两种方式:

被动生效
我们首先来看下我们熟悉的方式,通过SpringBoot的SPI的机制来去加载我们的starter。我们需要在META-INF下新建一个spring.factories文件key为org.springframework.boot.autoconfigure.EnableAutoConfiguration, value是我们的SmsAutoConfiguration 全限定名(记得去除前后的空格,否则会不生效)。
在这里插入图片描述
主动生效
在starter组件集成到我们的Spring Boot应用时需要主动声明启用该starter才生效,通过自定义一个@Enable注解然后在把自动配置类通过Import注解引入进来。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({SmsAutoConfiguration.class})
public @interface EnableSms {
}
使用的时候需要在启动类上面开启这个注解。
在这里插入图片描述
5.打包,部署到仓库
如果是本地的话,直接通过mvn install命令就可以了。
如果需要部署到公司的仓库话,这个就不说了。
6. 新建一个新的SpringBoot项目引入我们刚写的starter

com.workit.sms sms-spring-boot-starter 0.0.1-SNAPSHOT 在项目配置文件配上短信账号信息 在这里插入图片描述 测试代码

@SpringBootApplication
@EnableSms
public class AutoconfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(AutoconfigApplication.class, args);
AliyunSmsSenderImpl aliyunSmsSender = applicationContext.getBean(AliyunSmsSenderImpl.class);
aliyunSmsSender.send(“用阿里云发送短信”);
TencentSmsSenderImpl tencentSmsSender = applicationContext.getBean(TencentSmsSenderImpl.class);
tencentSmsSender.send(“用腾讯云发送短信”);
}
运行结果:

SmsMessage{userName=‘java金融’, passWord=‘12345’, sign=‘阿里云’, url=‘http://aliyun.com/send’}开始发送短信==》短信内容:用阿里云发送短信
SmsMessage{userName=‘java金融’, passWord=‘6666’, sign=‘腾讯云’, url=‘http://tencent.com/send’}开始发送短信==》短信内容:用腾讯云发送短信
至此的话我们自定义的一个starter就已经完成了,这个starter只是一个演示的demo,代码有点粗糙,项目结构也有点问题。重点看下这个实现原理就好。赶紧动动小手去实现一个自己的starter吧。

总结
SpringBoot starter的出现,让我们项目中集成其他组件变得简单。它把简单给了别人,把复杂留给了自己。“牺牲小我,成就大我”的思想还是值得学习的。平时我们工作中,比如要开发一个组件、或者一个工具类,也应该尽可能的让使用方可以做到无脑使用,不要搞的太复杂,又能让使用者可以灵活扩展。
结束
由于自己才疏学浅,难免会有纰漏,假如你发现了错误的地方,还望留言给我指出来,我会对其加以修正。
如果你觉得文章还不错,你的转发、分享、赞赏、点赞、留言就是对我最大的鼓励。
感谢您的阅读,十分欢迎并感谢您的关注。

全方位指导手把手教你实现自定义Spring Boot的 Starter公社相关推荐

  1. Spring Boot - 手把手教小师妹自定义Spring Boot Starter

    文章目录 Pre 自定义starter的套路 步骤 命名规范 官方命名空间 自定义命名空间 实战 创建一个父maven项目:springboot_custome_starter 创建 两个Module ...

  2. 手把手教你定制标准 Spring Boot starter

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...

  3. 手把手教你如何使用Spring Security(上):登录授权

    文章目录 一.什么是 Spring Security? 官方介绍 通俗来讲 二.初始搭建 创建 启动 三.项目原理 原理 思考 四.登录认证 登录过滤器 配置过滤器链 类补充 五.登录效果 效果演示 ...

  4. 手把手教你写一个spring IOC容器

    本文分享自华为云社区<手把手教你写一个spring IOC容器>,原文作者:技术火炬手. spring框架的基础核心和起点毫无疑问就是IOC,IOC作为spring容器提供的核心技术,成功 ...

  5. TensorFlow2 手把手教你实现自定义层

    TensorFlow2 手把手教你实现自定义层 概述 Sequential Model & Layer 案例 数据集介绍 完整代码 概述 通过自定义网络, 我们可以自己创建网络并和现有的网络串 ...

  6. 手把手教你 Tableau 自定义地理编码(十九)

    手把手教你 Tableau 自定义地理编码 Tableau 绘制地图时,支持的地理位置数据有限.当我们需要 Tableau 识别我们自定义的地理位置数据时,我们可以使用 Tableau 的自定义地理编 ...

  7. 快速开发一个自定义 Spring Boot Starter ,希望你也会

    来源:http://t.cn/Ai9li9fC 众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为 ...

  8. 自定义spring boot的自动配置

    文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...

  9. Spring Boot(3)---Spring Boot启动器Starter详解

    Spring Boot的启动器Starter详解 Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用, Spring Boot 管理 Spring 容器.第 ...

  10. 自定义 Spring Boot Starter

    一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...

最新文章

  1. 谈谈Koa 中的next
  2. 微信回调接口java返回true_java 微信支付异步回调接口
  3. 王昊奋 | 从聊天机器人到虚拟生命:AI技术的新机遇
  4. php按id获取整条数据库,Ajax取得数据库的json值,想通过id获取对应信息,但是都默认获取第一条了...
  5. bme280 环境传感器开发板_半导体所在柔性湿度传感器与非接触控制方面取得进展...
  6. android返回上一级代码,Android实践11 | 利用intent返回数据给上一级activity
  7. 老白聊数据-关于销售预测的那些事
  8. 用css实现文本不换行切超出限制时显示省略号(小tips)
  9. java过滤空号了停机号_手机空号、停机、注销,空号检测为你去除无效号码
  10. 反恐精英代码_知名网游源代码泄漏 ,外挂潮将来?
  11. forge下载java_我的世界forge1.9.4
  12. 推荐几个后台管理界面
  13. 【win10】win10开机黑屏时间长或只有鼠标解决办法,亲测可用
  14. 测试小故事48:想当然
  15. ElementUI导航菜单嵌套多级折叠面板的小箭头图标bug
  16. 纯干货!最全股票基础知识(上)
  17. 好家伙,查看系统日志时我捕获了一只发生概率小于万分之一的Bug
  18. ####好好好#####知识图谱上的双塔召回:阿里的IntentGC模型
  19. 前端大事记之几件大事
  20. 微信小程序----学生信息注册篇

热门文章

  1. css引用 svg图标库,svg 图标文件引入小技巧
  2. c语言short a=32768,C语言中short整型资料的范围“-32768——32767”中的“-32768”是如何确定的?...
  3. etcd系列深入浅出客户端
  4. js原生实现图片爆炸效果
  5. qq游戏坦大战服务器维护中,高手教你如何解决QQ游戏问题
  6. 【JZOJ 省选模拟】6691.六道剑「一念无量劫」
  7. 学习OpenCV——计算邻接区域列表(build_adjoin)
  8. 如何在透视表中同时显示客户编码和客户名称
  9. 酷站60个漂亮可用的外文站点欣赏(经典推荐)
  10. 债券收益率预测模型_利率预测模型系列之一:简单的N-S模型运用