小伙伴们曾经可能都经历过整天写着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
message:
email:
username: Aden
password: 123456
key: HJFHADJSFBDASFHUADSINF
api-url: http://blog.qiyuan.run
feishu:
user-name: Aden
pass-word: 654321
key: HFJKADSBFJKADSJFKADSNFAD
调用
@Autowired SendEmailMessageServiceImpl emailMessageService; @Autowired SendFeishuMessageServiceImpl feishuMessageService; public boolean sendEmail(String msg) { return emailMessageService.sendMessage(msg); } public boolean sendFeishu(String msg){ return feishuMessageService.sendMessage(msg); }
@Autowired
SendEmailMessageServiceImpl emailMessageService;
SendFeishuMessageServiceImpl feishuMessageService;
public boolean sendEmail(String msg) {
return emailMessageService.sendMessage(msg);
}
public boolean sendFeishu(String msg){
return feishuMessageService.sendMessage(msg);
效果的就是以上这样,只要通过配置需要发送消息的配置,自动注入发送消息的API,就可以实现发送消息了,以下是实现过程。
第一步,需要为你的starter取一个响亮的名字,spring的官方文档中说明,官方的 starter 的命名格式为 spring-boot-starter-{xxxx} 比如spring-boot-starter-activemq 第三方我们自己的命名格式为 {xxxx}-spring-boot-starter。比如mybatis-spring-boot-starter,此处,我命名为message-spring-boot-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")@Datapublic 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; }}
@ConfigurationProperties(prefix = "message")
@Data
public class MessageProperties {
/**
* 邮箱消息
*/
private MessageConfigInfo email = new MessageConfigInfo();
* 飞书消息
private MessageConfigInfo feishu = new MessageConfigInfo();
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 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 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; }}
public class SendFeishuMessageServiceImpl implements SendMessageService {
public SendFeishuMessageServiceImpl(MessageProperties messageProperties) {
@EnableConfigurationProperties(value = MessageProperties.class)@Configurationpublic 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); }}
@EnableConfigurationProperties(value = MessageProperties.class)
@Configuration
public class MessageAutoConfiguration {
* 给发送邮件的实现类,注入配置信息
* @param messageProperties
* @return
@Bean
public SendEmailMessageServiceImpl emailMessageConfig(MessageProperties messageProperties){
return new SendEmailMessageServiceImpl(messageProperties);
* 给发送飞书的实现类,注入配置信息
public SendFeishuMessageServiceImpl feishuMessageConfig(MessageProperties messageProperties){
return new SendFeishuMessageServiceImpl(messageProperties);
org.springframework.boot.autoconfigure.EnableAutoConfiguration=run.qiyuan.message.config.MessageAutoConfiguration
编写完之后,mvn install即可。
mvn install
在我们的项目中,引入自定义starter的坐标
<dependency> <groupId>run.qiyuan</groupId> <artifactId>message-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
<dependency>
<groupId>run.qiyuan</groupId>
<artifactId>message-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
然后在配置文件中配置相关的信息
测试
@SpringBootApplicationpublic 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!,这是一封飞书信息!"); }}
@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!,这是一封飞书信息!");
结果
原文链接:https://www.cnblogs.com/onecoder/p/16894269.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728