利用Spring插件實(shí)現(xiàn)策略模式的案例詳解
前言
偶然的機(jī)會(huì)發(fā)現(xiàn)spring有個(gè)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插件提供了一種更實(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,因此無需指定版本號。不過集成的版本號比較低,而且部分方法與高版本不兼容
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("來自阿里云短信:" + 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、測試
@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來實(shí)現(xiàn)策略模式。如果我們對擴(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea啟動(dòng)報(bào)錯(cuò):Command line is too long問題
在使用IDEA時(shí),若遇到"Commandlineistoolong"錯(cuò)誤,通常是因?yàn)槊钚虚L度超限,這是因?yàn)镮DEA通過命令行或文件將classpath傳遞至JVM,操作系統(tǒng)對命令行長度有限制,解決方法是切換至動(dòng)態(tài)類路徑,通過修改項(xiàng)目的workspace.xml文件2024-09-09
Spring中的ThreadPoolTaskExecutor線程池使用詳解
這篇文章主要介紹了Spring中的ThreadPoolTaskExecutor線程池使用詳解,ThreadPoolTaskExecutor 是 Spring框架提供的一個(gè)線程池實(shí)現(xiàn),用于管理和執(zhí)行多線程任務(wù),它是TaskExecutor接口的實(shí)現(xiàn),提供了在 Spring 應(yīng)用程序中創(chuàng)建和配置線程池的便捷方式,需要的朋友可以參考下2024-01-01
詳解Java如何優(yōu)雅的實(shí)現(xiàn)異常捕獲
在一個(gè)優(yōu)秀的項(xiàng)目中一定少不了對程序流程良好的異常捕獲與日志打印,所以本文主要為大家介紹了如何優(yōu)雅的實(shí)現(xiàn)異常捕獲與日志打印輸出,有需要的可以參考下2023-09-09
解決Maven項(xiàng)目pom.xml導(dǎo)入了Junit包還是用不了@Test注解問題
在Maven項(xiàng)目中,如果在非test目錄下使用@Test注解,可能會(huì)因?yàn)閜om.xml中<scope>test</scope>的設(shè)置而無法使用,正確做法是將測試代碼放在src/test/java目錄下,或去除<scope>test</scope>限制,這樣可以確保Junit依賴正確加載并應(yīng)用于適當(dāng)?shù)拇a部分2024-10-10
跟我學(xué)Java Swing之游戲設(shè)計(jì)(1)
跟我學(xué)Java Swing之游戲設(shè)計(jì)(1)...2006-12-12
了解java架構(gòu)之微服務(wù)架構(gòu)—雪崩效應(yīng)
這篇文章主要介紹了了解java架構(gòu)之微服務(wù)架構(gòu)—雪崩效應(yīng),微服務(wù)化產(chǎn)品線,每一個(gè)服務(wù)專心于自己的業(yè)務(wù)邏輯,并對外提供相應(yīng)的接口,看上去似乎很明了,其實(shí)還有很多的東西需要考慮,,需要的朋友可以參考下2019-06-06
java發(fā)送javax.mail郵件實(shí)例講解
這篇文章主要為大家介紹了java發(fā)送javax.mail郵件實(shí)例講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01

