详解SpringBoot如何自定义一个Starter
一只小coder 人气:0需求
在一个项目中,用户需要发送消息,可以通过邮件,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!,这是一封飞书信息!"); } }
加载全部内容