小伙伴们曾经可能都经历过整天写着CURD的业务,都没写过一些组件相关的东西,这篇文章记录一下SpringBoot如何自定义一个Starter。

原理和理论就不用多说了,可以在网上找到很多关于该方面的资料,这里主要分享如何自定义。

原文链接:SpringBoot怎么自定义一个Starter ?一只小Coder

需求

在一个项目中,用户需要发送消息,可以通过邮件,QQ,微信,钉钉,飞书等,目前这些发送消息的方式都已经提供了公开的API,想要实现在项目中通过简单的配置各个发发送方的发送方信息,然后直接调用发送的API,发送信息即可,下面举个:

配置

message:
email:
username: Aden
password: 123456
key: HJFHADJSFBDASFHUADSINF
api-url: http://blog.qiyuan.run
feishu:
user-name: Aden
pass-word: 654321
key: HFJKADSBFJKADSJFKADSNFAD
api-url: http://blog.qiyuan.run

调用

    @Autowired
SendEmailMessageServiceImpl emailMessageService; @Autowired
SendFeishuMessageServiceImpl feishuMessageService; public boolean sendEmail(String msg) {
return emailMessageService.sendMessage(msg);
} public boolean sendFeishu(String msg){
return feishuMessageService.sendMessage(msg);
}

效果的就是以上这样,只要通过配置需要发送消息的配置,自动注入发送消息的API,就可以实现发送消息了,以下是实现过程。

starter创建

第一步,需要为你的starter取一个响亮的名字,spring的官方文档中说明,官方的 starter 的命名格式为 spring-boot-starter-{xxxx} 比如spring-boot-starter-activemq

第三方我们自己的命名格式为 {xxxx}-spring-boot-starter。比如mybatis-spring-boot-starter,此处,我命名为message-spring-boot-starter

自定义配置信息类

因为要在项目中的配置文件中写配置信息,所以在这个starter中,我们需要通过一个配置信息类来接收配置的信息。

@ConfigurationProperties(prefix = "message")
@Data
public class MessageProperties {
/**
* 邮箱消息
*/
private MessageConfigInfo email = new MessageConfigInfo();
/**
* 飞书消息
*/
private MessageConfigInfo feishu = new MessageConfigInfo(); @Data
public static class MessageConfigInfo {
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String passWord;
/**
* 秘钥
*/
private String key;
/**
* 消息发送API
*/
private String apiUrl;
}
}

发送消息的实现

由于需要通过这个starter实现发送消息,所以这里可能得要引入发送邮件,发送飞书的官方API,这里就不搞这么复杂了,主要还是看过程,自定义一个接口模拟一下即可。

模拟接口定义

public interface SendMessageService {
Boolean sendMessage(String message);
}

模拟接口实现

public class SendEmailMessageServiceImpl implements SendMessageService {

    private MessageProperties messageProperties;

    public SendEmailMessageServiceImpl(MessageProperties messageProperties) {
this.messageProperties = messageProperties;
} @Override
public Boolean sendMessage(String message) {
System.out.println(messageProperties.toString() + " 开发发送邮件,发送内容为:" + message);
return true;
}
}
public class SendFeishuMessageServiceImpl implements SendMessageService {

    private MessageProperties messageProperties;

    public SendFeishuMessageServiceImpl(MessageProperties messageProperties) {
this.messageProperties = messageProperties;
} @Override
public Boolean sendMessage(String message) {
System.out.println(messageProperties.toString() + " 开发发送邮件,发送内容为:" + message);
return true;
}
}

自动配置类

@EnableConfigurationProperties(value = MessageProperties.class)
@Configuration
public class MessageAutoConfiguration {
/**
* 给发送邮件的实现类,注入配置信息
* @param messageProperties
* @return
*/
@Bean
public SendEmailMessageServiceImpl emailMessageConfig(MessageProperties messageProperties){
return new SendEmailMessageServiceImpl(messageProperties);
} /**
* 给发送飞书的实现类,注入配置信息
* @param messageProperties
* @return
*/
@Bean
public SendFeishuMessageServiceImpl feishuMessageConfig(MessageProperties messageProperties){
return new SendFeishuMessageServiceImpl(messageProperties);
}
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=run.qiyuan.message.config.MessageAutoConfiguration

项目结构

编写完之后,mvn install即可。

如何使用该starter

在我们的项目中,引入自定义starter的坐标

        <dependency>
<groupId>run.qiyuan</groupId>
<artifactId>message-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

然后在配置文件中配置相关的信息

message:
email:
username: Aden
password: 123456
key: HJFHADJSFBDASFHUADSINF
api-url: http://blog.qiyuan.run
feishu:
user-name: Aden
pass-word: 654321
key: HFJKADSBFJKADSJFKADSNFAD
api-url: http://blog.qiyuan.run

测试

@SpringBootApplication
public class TeachApplication { public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TeachApplication.class, args);
SendEmailMessageServiceImpl emailMessageService = context.getBean(SendEmailMessageServiceImpl.class);
emailMessageService.sendMessage("你好,Starter!,这是一封邮件信息!\n\n");
SendFeishuMessageServiceImpl feishuMessageService = context.getBean(SendFeishuMessageServiceImpl.class);
feishuMessageService.sendMessage("你好,Starter!,这是一封飞书信息!");
}
}

结果

SpringBoot怎么自定义一个Starter ?的更多相关文章

  1. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  2. Spring boot 自定义一个starter pom

    用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...

  3. SpringBoot自定义一个starter

    @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 ...

  4. 尚硅谷springboot学习36-自定义starter

    自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @Cond ...

  5. springboot系列十四、自定义实现starter

    一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...

  6. springBoot 自动配置原理--自己新建一个 starter

    上篇我们说到 springboot 和 SSM 框架的区别,今天我们就看看 springboot 到底为我们做了哪些事情,让我们开发变得如此简单. springboot 中起着重要作用的是 start ...

  7. 深入springboot原理——动手封装一个starter

    从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...

  8. 【SpringBoot】编写一个自己的Starter

    一.什么是Starter? 在开发过程中我们就经常使用到各种starter,比如mybatis-spring-boot-starter,只需要进行简单的配置即可使用,就像一个插件非常方便.这也是Spr ...

  9. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  10. springboot2.x基础教程:动手制作一个starter包

    上一篇博客介绍了springboot自动装配的原理.springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的 ...

随机推荐

  1. OSI七层模型与TCP/IP协议

    作者:菘蓝 时间:2022/9/1 ================================================================================== ...

  2. CentOS7_K8S安装指南

    https://www.cnblogs.com/liu-shuai/articles/12177298.html 不能完全按照他来装,因为他装的是15.5的,15.5 有部分组件在阿里云镜像上没有,导 ...

  3. [BJDCTF2020]Easy MD5 WP

    老样子 打开看看 你会发现 啥也没有 有一个输入框,随便输入看看 抓包发现跳转leveldo4.php 同时看到 hint 里面有select * from 'admin' where passwor ...

  4. eclipse最常应用的几个快捷键,新手必看!

    首先eclipse快捷键可以使用 Ctrl + Shift + L 打开,在这里可以查看所有快捷键. 另外就是常用的几个快捷键 选中光标所在行 好像没有,但是可以 Ctrl + d 然后 Ctrl + ...

  5. vue3 的 ref、isRef、toRef、toRefs、toRaw 详细介绍

    ref.isRef.toRef.toRefs.toRaw 看着一堆类似的东西,一个头两个大,今天整理一篇文章详细介绍它们的功能及区别. 1.ref ref 属性除了能够获取元素外,也可以使用 ref ...

  6. Nginx相关模块学习使用实践指南

    转载自:https://www.bilibili.com/read/cv16150654?spm_id_from=333.999.0.0 0x01 Nginx 常用模块使用实践 官方模块使用手册:ht ...

  7. MySQL8 二进制日志

    启用二进制日志 # cat /etc/my.cnf [mysqld] server_id=100 log_bin=/var/log/mysql/binlogs/server1 # mkdir -p / ...

  8. owncloud安装部署手册

    Dockerhub地址:https://hub.docker.com/r/owncloud/server docker pull owncloud/server:10.9.0 docker run - ...

  9. 使用Filebeat传送多行日志

    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/106272704 在解决应用程序问题时,多行日志为开发人员提供了宝贵的信息. 堆栈跟踪 ...

  10. 多字段特性及配置自定义Analyzer

    PUT logs/_doc/1 {"level":"DEBUG"} GET /logs/_mapping POST _analyze { "token ...