利用Spring插件實現(xiàn)策略模式的案例詳解
前言
偶然的機(jī)會發(fā)現(xiàn)spring有個spring-plugin,官網(wǎng)對它的介紹是
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)擴(kuò)展核心系統(tǒng)功能的核心靈活性,但當(dāng)然不提供核心OSGi功能,如動態(tài)類加載或運行時安裝和部署插件。盡管Spring插件因此不如OSGi強大,但它滿足了窮人構(gòu)建模塊化可擴(kuò)展應(yīng)用程序的需求。
使用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以下版本默認(rèn)已經(jīng)集成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è)務(wù)項目注入
@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、測試
@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é)
本文主要通過一個模擬短信發(fā)送的示例,演示如何通過spring-plugin來實現(xiàn)策略模式。如果我們對擴(kuò)展性有要求除了spi,我們也可以考慮使用spring-plugin。不過基于spring-plugin擴(kuò)展時,要注意具體的插件實現(xiàn)類要為spring的bean,不然插件會找不到
到此這篇關(guān)于利用Spring插件實現(xiàn)策略模式的案例詳解的文章就介紹到這了,更多相關(guān)Spring插件實現(xiàn)策略模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié)
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,本文主要介紹了SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-05-05