聊聊如何利用spring插件來實現(xiàn)策略模式

前言

偶然的機會發(fā)現(xiàn)spring有個spring-plugin矾麻,官網對它的介紹是

Spring Plugin provides a more pragmatic approach to plugin development by providing the core flexibility of having plugin implementations extending a core system's functionality but of course not delivering core OSGi features like dynamic class loading or runtime installation and deployment of plugins. Although Spring Plugin thus is not nearly as powerful as OSGi, it serves a poor man's requirements to build a modular extensible application.

大意就是Spring插件提供了一種更實用的插件開發(fā)方法,它提供了插件實現(xiàn)擴展核心系統(tǒng)功能的核心靈活性吏颖,但當然不提供核心OSGi功能输莺,如動態(tài)類加載或運行時安裝和部署插件甫男。盡管Spring插件因此不如OSGi強大攘已,但它滿足了窮人構建模塊化可擴展應用程序的需求。

本文就來聊下如何使用spring插件來實現(xiàn)策略模式

使用spring-plugin插件實現(xiàn)策略模式步驟

1澜共、在項目中的pom引入spring-plugin

 <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>2.0.0.RELEASE<version>
        </dependency>

注: springboot 2.2以下版本默認已經集成spring-plugin-core向叉,因此無需指定版本號。不過集成的版本號比較低嗦董,而且部分方法與高版本不兼容

2母谎、定義一個實體類,這個實體類后邊插件綁定插件類型會用到

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SmsRequest implements Serializable {

    private Map<String,Object> metaDatas;

    private String to;

    private String message;

    private SmsType smsType;


}

3京革、定義插件實現(xiàn)org.springframework.plugin.core.Plugin接口

public interface SmsPlugin extends Plugin<SmsRequest> {


    SmsResponse sendSms(SmsRequest smsRequest);


}

4奇唤、配置激活插件

@EnablePluginRegistries(SmsPlugin.class)
@Configuration
public class SmsPluginActiveConfig {

}

5、定義插件的具體實現(xiàn)類

@Component
public class AliyunSmsPlugin implements SmsPlugin {
    @Override
    public SmsResponse sendSms(SmsRequest smsRequest) {
        System.out.println("來自阿里云短信:" + smsRequest);
        return SmsResponse.builder()
                .code("200").message("發(fā)送成功")
                .success(true).result("阿里云短信的回執(zhí)").build();
    }

    @Override
    public boolean supports(SmsRequest smsRequest) {
        return SmsType.ALIYUN == smsRequest.getSmsType();
    }
}

注:該具體插件必須是spring的bean

6匹摇、插件使用

在業(yè)務項目注入

@Autowired
private PluginRegistry<SmsPlugin,SmsRequest> pluginRegistry;

通用調用pluginRegistry.getPluginFor方法拿到具體插件

示例

@RequiredArgsConstructor
public class SmsService {


    private final PluginRegistry<SmsPlugin,SmsRequest> pluginRegistry;


    public SmsResponse sendSms(SmsRequest smsRequest){
        Optional<SmsPlugin> smsPlugin = pluginRegistry.getPluginFor(smsRequest);
        return smsPlugin.orElseThrow(() -> new SmsException("Sms plugin is not binder with type : 【" + smsRequest.getSmsType() + "】"))
                .sendSms(smsRequest);


    }
}

7冻记、測試

 @Test
    public void testAliyunSms(){
        SmsRequest smsRequest = SmsRequest.builder()
                .message("模擬使用阿里云短信發(fā)送")
                .to("136000000001")
                .smsType(SmsType.ALIYUN)
                .build();

        SmsResponse smsResponse = smsService.sendSms(smsRequest);
        Assert.assertTrue(smsResponse.isSuccess());
        System.out.println(smsResponse);

    }
ba353e0e3c587c8dfb39375ff85b145e_0014b9b24fd4beb904b44fbd037618f0.png

總結

本文主要通過一個模擬短信發(fā)送的示例,演示如何通過spring-plugin來實現(xiàn)策略模式来惧。如果我們對擴展性有要求除了spi,我們也可以考慮使用spring-plugin演顾。不過基于spring-plugin擴展時供搀,要注意具體的插件實現(xiàn)類要為spring的bean,不然插件會找不到

更多詳細例子可以查看官網

https://github.com/spring-projects/spring-plugin

demo鏈接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-springplugin-strategy

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末钠至,一起剝皮案震驚了整個濱河市葛虐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌棉钧,老刑警劉巖屿脐,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡的诵,警方通過查閱死者的電腦和手機万栅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來西疤,“玉大人烦粒,你說我怎么就攤上這事〈蓿” “怎么了扰她?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長芭碍。 經常有香客問我徒役,道長,這世上最難降的妖魔是什么窖壕? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任忧勿,我火速辦了婚禮,結果婚禮上艇拍,老公的妹妹穿的比我還像新娘狐蜕。我一直安慰自己,他們只是感情好卸夕,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布层释。 她就那樣靜靜地躺著,像睡著了一般快集。 火紅的嫁衣襯著肌膚如雪贡羔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天个初,我揣著相機與錄音乖寒,去河邊找鬼。 笑死院溺,一個胖子當著我的面吹牛楣嘁,可吹牛的內容都是我干的。 我是一名探鬼主播珍逸,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼逐虚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谆膳?” 一聲冷哼從身側響起叭爱,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎漱病,沒想到半個月后买雾,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體把曼,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年漓穿,在試婚紗的時候發(fā)現(xiàn)自己被綠了嗤军。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡器净,死狀恐怖型雳,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情山害,我是刑警寧澤纠俭,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站浪慌,受9級特大地震影響冤荆,放射性物質發(fā)生泄漏。R本人自食惡果不足惜权纤,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一钓简、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧汹想,春花似錦外邓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至槽唾,卻和暖如春丧枪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背庞萍。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工拧烦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钝计。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓恋博,卻偏偏與公主長得像,于是被迫代替她去往敵國和親私恬。 傳聞我的和親對象是個殘疾皇子交播,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容