手把手教你实现自定义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.
❞
通过 引入jdbc的依赖、以及jpa相关的各种依赖编写 相关的配置文件网上各种查询找资料进行调试,调试的过程对于新手可能会有点奔溃会遇到各种奇奇怪怪的问题, 包冲突啊,这个包下载不下来,缺少某个包。终于在经历千辛万苦,哼次哼次的解决各种问题之后终于把项目跑起来了,然后把这次整合 遇到的问题,以及整合的步骤都一一的详细记录下来。方便下次在需要整合的时候直接就好了。我们以前在没有之前是不是都是这么玩的。这样的缺点是不是也非常显著,比如过程复杂、需要不停的粘贴复制(不过这是程序员经常干的事情了,也不在乎多一两次了)、整合其它组件到自己的项目变的困难,效率低下。这也就造成了的程序员比较多了(晚上就不能够回去了)。

SpringBoot Starter的出现

的实现:虽然我们每个组件的实现各有差异,但是它们基本上都会使用到两个相同的内容:和。因为提倡“
实现自己的SpringBoot Starter
命名规范
❝
What’s in a nameAll 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
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
1.16.18
provided
@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;
@Override
public String toString() {
return "SmsMessage{" +
"userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", sign='" + sign + '\'' +
", url='" + url + '\'' +
'}';
}
}
}
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

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

@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。我们需要在下新建一个文件为是我们的 全限定名(「记得去除前后的空格,否则会不生效」 )。

主动生效在 组件集成到我们的应用时需要主动声明启用该才生效,通过自定义一个注解然后在把自动配置类通过注解引入进来。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({SmsAutoConfiguration.class})
public @interface EnableSms {
}

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

站在巨人的肩膀上摘苹果:https://www.cnblogs.com/tjudzj/p/8758391.htmlhttps://blog.springlearn.cn/posts/14644/