最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot如何自定義一個(gè)Starter

 更新時(shí)間:2022年11月16日 08:22:14   作者:一只小coder  
小伙伴們?cè)?jīng)可能都經(jīng)歷過整天寫著CURD的業(yè)務(wù),都沒寫過一些組件相關(guān)的東西,這篇文章記錄一下SpringBoot如何自定義一個(gè)Starter。原理和理論就不用多說了,可以在網(wǎng)上找到很多關(guān)于該方面的資料,這里主要分享如何自定義

需求

在一個(gè)項(xiàng)目中,用戶需要發(fā)送消息,可以通過郵件,QQ,微信,釘釘,飛書等,目前這些發(fā)送消息的方式都已經(jīng)提供了公開的API,想要實(shí)現(xiàn)在項(xiàng)目中通過簡單的配置各個(gè)發(fā)發(fā)送方的發(fā)送方信息,然后直接調(diào)用發(fā)送的API,發(fā)送信息即可,下面舉個(gè)例子:

配置

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

調(diào)用

    @Autowired
    SendEmailMessageServiceImpl emailMessageService;
    
    @Autowired
    SendFeishuMessageServiceImpl feishuMessageService;

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

效果的就是以上這樣,只要通過配置需要發(fā)送消息的配置,自動(dòng)注入發(fā)送消息的API,就可以實(shí)現(xiàn)發(fā)送消息了,以下是實(shí)現(xiàn)過程。

starter創(chuàng)建

第一步,需要為你的starter取一個(gè)響亮的名字,spring的官方文檔中說明,官方的 starter 的命名格式為 spring-boot-starter-{xxxx} 比如spring-boot-starter-activemq第三方我們自己的命名格式為 {xxxx}-spring-boot-starter。比如mybatis-spring-boot-starter,此處,我命名為message-spring-boot-starter

自定義配置信息類

因?yàn)橐陧?xiàng)目中的配置文件中寫配置信息,所以在這個(gè)starter中,我們需要通過一個(gè)配置信息類來接收配置的信息。

@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;
        /**
         * 消息發(fā)送API
         */
        private String apiUrl;
    }
}

發(fā)送消息的實(shí)現(xiàn)

由于需要通過這個(gè)starter實(shí)現(xiàn)發(fā)送消息,所以這里可能得要引入發(fā)送郵件,發(fā)送飛書的官方API,這里就不搞這么復(fù)雜了,主要還是看過程,自定義一個(gè)接口模擬一下即可。

模擬接口定義

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

模擬接口實(shí)現(xiàn)

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() + "  開發(fā)發(fā)送郵件,發(fā)送內(nèi)容為:" + 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() + "  開發(fā)發(fā)送郵件,發(fā)送內(nèi)容為:" + message);
        return true;
    }
}

自動(dòng)配置類

@EnableConfigurationProperties(value = MessageProperties.class)
@Configuration
public class MessageAutoConfiguration {
    /**
     * 給發(fā)送郵件的實(shí)現(xiàn)類,注入配置信息
     * @param messageProperties
     * @return
     */
    @Bean
    public SendEmailMessageServiceImpl emailMessageConfig(MessageProperties messageProperties){
        return new SendEmailMessageServiceImpl(messageProperties);
    }


    /**
     * 給發(fā)送飛書的實(shí)現(xiàn)類,注入配置信息
     * @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

項(xiàng)目結(jié)構(gòu)

編寫完之后,mvn install即可。

如何使用該starter

在我們的項(xiàng)目中,引入自定義starter的坐標(biāo)

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

然后在配置文件中配置相關(guān)的信息

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如何自定義一個(gè)Starter的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot自定義Starter的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用jib插件為Java應(yīng)用構(gòu)建鏡像的方法

    使用jib插件為Java應(yīng)用構(gòu)建鏡像的方法

    這篇文章主要介紹了使用jib插件為Java應(yīng)用構(gòu)建鏡像,要是用戶本地沒安裝docker,可以使用jib制作出帶有鏡像的tar文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • CentOS7和8中安裝Maven3.8.4的簡單步驟

    CentOS7和8中安裝Maven3.8.4的簡單步驟

    maven是屬于apache的一個(gè)工具,主要是對(duì)java進(jìn)行編譯打包,解決依賴關(guān)系,下面這篇文章主要給大家介紹了關(guān)于CentOS7和8中安裝Maven3.8.4的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 一文帶你搞懂Java中的泛型和通配符

    一文帶你搞懂Java中的泛型和通配符

    泛型機(jī)制在項(xiàng)目中一直都在使用,甚至很多源碼中都用到了泛型機(jī)制。但是里面很多的機(jī)制和特性一直沒有明白,尤其通配符這塊,經(jīng)常忘記。本文對(duì)此做了一些總結(jié),具有一定借鑒價(jià)值,希望有所幫助
    2022-09-09
  • JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能

    JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能

    這篇文章主要為大家詳細(xì)介紹了JavaWeb郵件發(fā)送接收功能的實(shí)現(xiàn),郵件發(fā)送和接收功能是非常常用的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • SWT(JFace)體驗(yàn)之List演示匯總

    SWT(JFace)體驗(yàn)之List演示匯總

    SWT(JFace)體驗(yàn)之List演示代碼匯總
    2009-06-06
  • java字符串反轉(zhuǎn)的7種方法

    java字符串反轉(zhuǎn)的7種方法

    本文主要介紹了java字符串反轉(zhuǎn)的7種方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 一文深入理解Java中的深拷貝機(jī)制

    一文深入理解Java中的深拷貝機(jī)制

    在Java編程中,我們經(jīng)常需要處理對(duì)象的復(fù)制問題,深拷貝和淺拷貝是兩種常見的復(fù)制方式,它們?cè)趦?nèi)存管理和對(duì)象引用方面存在不同特點(diǎn),本文將帶大家深入探究Java中的深拷貝機(jī)制,需要的朋友可以參考下
    2023-09-09
  • JDBC獲取元數(shù)據(jù)demo

    JDBC獲取元數(shù)據(jù)demo

    這篇文章主要為大家介紹了JDBC獲取元數(shù)據(jù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Filter在springboot中的使用方法詳解

    Filter在springboot中的使用方法詳解

    這篇文章主要介紹了Filter在springboot中的使用方法詳解,filter(過濾器)作用于在intreceptor(攔截器)之前,不像intreceptor一樣依賴于springmvc框架,只需要依賴于serverlet,需要的朋友可以參考下
    2023-08-08
  • 全網(wǎng)最精細(xì)詳解二叉樹,2萬字帶你進(jìn)入算法領(lǐng)域

    全網(wǎng)最精細(xì)詳解二叉樹,2萬字帶你進(jìn)入算法領(lǐng)域

    大家好,我是哪吒,一個(gè)熱愛編碼的Java工程師,本著"欲速則不達(dá),欲達(dá)則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時(shí)間慢慢擦亮你的眼睛,少時(shí)看重的,年長后卻視若鴻毛,少時(shí)看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程
    2021-08-08

最新評(píng)論

柳河县| 泸西县| 子长县| 崇仁县| 甘德县| 大埔县| 安阳市| 鄂伦春自治旗| 南陵县| 德保县| 平昌县| 迁西县| 通州区| 雷波县| 清水县| 上思县| 伊吾县| 新绛县| 冕宁县| 景宁| 衡水市| 郁南县| 融水| 库伦旗| 鄂温| 丘北县| 芜湖市| 专栏| 嘉兴市| 论坛| 澄迈县| 剑河县| 肃南| 四平市| 盐边县| 平阴县| 安乡县| 巍山| 河东区| 临漳县| 嘉禾县|