小伙伴们曾经可能都经历过整天写着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. Android Notification使用

    一 Notification的类别 1.状态栏和抽屉式通知 //获取NotificationManager对象 val notificationManager = getSystemService(N ...

  2. KingbaseES 数据库参数优化

    一.数据库应用类型 针对不同的应用模型,需要对数据库配置进行优化: 1.网络应用程序(WEB) ​通常受 CPU 限制 DB比RAM小得多 90% 或更多的简单查询 2.在线事务处理 (OLTP) ​ ...

  3. JS 模块化 - 02 Common JS 模块化规范

    1 Common JS 介绍 Common JS 是模块化规范之一.每个文件都是一个作用域,文件里面定义的变量/函数都是私有的,对其他模块不可见.Common JS 规范在 Node 端和浏览器端有不 ...

  4. Java容器化参数配置最佳实践

    Java是以VM为基础的,而云原生讲究的就是Native,天然的矛盾,虽然Quarkus是为GraalVM和HotSpot量身定制的K8s Native Java框架,生态原因切换成本太高,这种矛盾体 ...

  5. Traefik2.X 版本 中 URL Rewrite 的使用

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484594&idx=1&sn=becbe567 ...

  6. Redis 监控指标

    监控指标 性能指标:Performance 内存指标: Memory 基本活动指标:Basic activity 持久性指标: Persistence 错误指标:Error 性能指标:Performa ...

  7. 迁移一个仓库到新的Gitlab

    一般这种迁移,要注意旧仓库的提交历史等信息也要同步到新的仓库. 先使用如下命令克隆老的: git clone --bare git@gitlab.test1.com:f2e/test.git 新仓库创 ...

  8. 4_Spring

    一. Spring Spring的基本组成: 1.最完善的轻量级核心框架. 2.通用的事务管理抽象层. 3.JDBC抽象层. 4.集成了Toplink, Hibernate, JDO, and iBA ...

  9. Go微服务实战 - 用户服务开发(gRPC+Protocol Buffer)

    概要 用户服务基本是每个互联网产品里必备的一个服务了,因为没有用户基本是什么也干不了.所以他的重要性不言而喻.本文主要介绍下如何开发一个用户微服务,以及他的详细开发流程. 目录 Go微服务实战 - 从 ...

  10. HBase(1/5)

    HBase学习(一) 一.了解HBase 官方文档:https://hbase.apache.org/book.html 1.1 HBase概述 HBase 是一个高可靠性.高性能.面向列.可伸缩的分 ...