利用Spring插件实现策略模式的案例详解
前言
偶然的机会发现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插件提供了一种更实用的插件开发方法,它提供了插件实现扩展核心系统功能的核心灵活性,但当然不提供核心OSGi功能,如动态类加载或运行时安装和部署插件。尽管Spring插件因此不如OSGi强大,但它满足了穷人构建模块化可扩展应用程序的需求。
使用spring-plugin插件实现策略模式步骤
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、定义插件实现org.springframework.plugin.core.Plugin接口
public interface SmsPlugin extends Plugin<SmsRequest> {
SmsResponse sendSms(SmsRequest smsRequest);
}4、配置激活插件
@EnablePluginRegistries(SmsPlugin.class)
@Configuration
public class SmsPluginActiveConfig {
}5、定义插件的具体实现类
@Component
public class AliyunSmsPlugin implements SmsPlugin {
@Override
public SmsResponse sendSms(SmsRequest smsRequest) {
System.out.println("来自阿里云短信:" + smsRequest);
return SmsResponse.builder()
.code("200").message("发送成功")
.success(true).result("阿里云短信的回执").build();
}
@Override
public boolean supports(SmsRequest smsRequest) {
return SmsType.ALIYUN == smsRequest.getSmsType();
}
}注:该具体插件必须是spring的bean
6、插件使用
在业务项目注入
@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("模拟使用阿里云短信发送")
.to("136000000001")
.smsType(SmsType.ALIYUN)
.build();
SmsResponse smsResponse = smsService.sendSms(smsRequest);
Assert.assertTrue(smsResponse.isSuccess());
System.out.println(smsResponse);
}
总结
本文主要通过一个模拟短信发送的示例,演示如何通过spring-plugin来实现策略模式。如果我们对扩展性有要求除了spi,我们也可以考虑使用spring-plugin。不过基于spring-plugin扩展时,要注意具体的插件实现类要为spring的bean,不然插件会找不到
到此这篇关于利用Spring插件实现策略模式的案例详解的文章就介绍到这了,更多相关Spring插件实现策略模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
idea启动报错:Command line is too long问题
在使用IDEA时,若遇到"Commandlineistoolong"错误,通常是因为命令行长度超限,这是因为IDEA通过命令行或文件将classpath传递至JVM,操作系统对命令行长度有限制,解决方法是切换至动态类路径,通过修改项目的workspace.xml文件2024-09-09
Spring中的ThreadPoolTaskExecutor线程池使用详解
这篇文章主要介绍了Spring中的ThreadPoolTaskExecutor线程池使用详解,ThreadPoolTaskExecutor 是 Spring框架提供的一个线程池实现,用于管理和执行多线程任务,它是TaskExecutor接口的实现,提供了在 Spring 应用程序中创建和配置线程池的便捷方式,需要的朋友可以参考下2024-01-01
解决Maven项目pom.xml导入了Junit包还是用不了@Test注解问题
在Maven项目中,如果在非test目录下使用@Test注解,可能会因为pom.xml中<scope>test</scope>的设置而无法使用,正确做法是将测试代码放在src/test/java目录下,或去除<scope>test</scope>限制,这样可以确保Junit依赖正确加载并应用于适当的代码部分2024-10-10


最新评论