利用Spring插件實(shí)現(xiàn)策略模式的案例詳解
前言
偶然的機(jī)會(huì)發(fā)現(xiàn)spring有個(gè)spring-plugin,官網(wǎng)對(duì)它的介紹是
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插件提供了一種更實(shí)用的插件開發(fā)方法,它提供了插件實(shí)現(xiàn)擴(kuò)展核心系統(tǒng)功能的核心靈活性,但當(dāng)然不提供核心OSGi功能,如動(dòng)態(tài)類加載或運(yùn)行時(shí)安裝和部署插件。盡管Spring插件因此不如OSGi強(qiáng)大,但它滿足了窮人構(gòu)建模塊化可擴(kuò)展應(yīng)用程序的需求。
使用spring-plugin插件實(shí)現(xiàn)策略模式步驟
1、在項(xiàng)目中的pom引入spring-plugin
<dependency> <groupId>org.springframework.plugin</groupId> <artifactId>spring-plugin-core</artifactId> <version>2.0.0.RELEASE<version> </dependency>
注: springboot 2.2以下版本默認(rèn)已經(jīng)集成spring-plugin-core,因此無(wú)需指定版本號(hào)。不過集成的版本號(hào)比較低,而且部分方法與高版本不兼容
2、定義一個(gè)實(shí)體類,這個(gè)實(shí)體類后邊插件綁定插件類型會(huì)用到
@Data @AllArgsConstructor @NoArgsConstructor @Builder public class SmsRequest implements Serializable { private Map<String,Object> metaDatas; private String to; private String message; private SmsType smsType; }
3、定義插件實(shí)現(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、定義插件的具體實(shí)現(xiàn)類
@Component public class AliyunSmsPlugin implements SmsPlugin { @Override public SmsResponse sendSms(SmsRequest smsRequest) { System.out.println("來(lái)自阿里云短信:" + 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è)務(wù)項(xiàng)目注入
@Autowired private PluginRegistry<SmsPlugin,SmsRequest> pluginRegistry;
通用調(diào)用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、測(cè)試
@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); }
總結(jié)
本文主要通過一個(gè)模擬短信發(fā)送的示例,演示如何通過spring-plugin來(lái)實(shí)現(xiàn)策略模式。如果我們對(duì)擴(kuò)展性有要求除了spi,我們也可以考慮使用spring-plugin。不過基于spring-plugin擴(kuò)展時(shí),要注意具體的插件實(shí)現(xiàn)類要為spring的bean,不然插件會(huì)找不到
到此這篇關(guān)于利用Spring插件實(shí)現(xiàn)策略模式的案例詳解的文章就介紹到這了,更多相關(guān)Spring插件實(shí)現(xiàn)策略模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot啟動(dòng)后立即執(zhí)行的幾種方法小結(jié)
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,本文主要介紹了SpringBoot啟動(dòng)后立即執(zhí)行的幾種方法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05Quarkus改造Pmml模型項(xiàng)目異常記錄及解決處理
這篇文章主要為大家介紹了Quarkus改造Pmml模型項(xiàng)目是遇到的異常記錄以及解決方法,有需要的同學(xué)可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息詳解
這篇文章主要給大家介紹了關(guān)于如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01