为了账号安全,请及时绑定邮箱和手机立即绑定

聊聊如何利用spring插件来实现策略模式

标签:
Java 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插件来实现策略模式

使用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);

    }

ba353e0e3c587c8dfb39375ff85b145e_0014b9b24fd4beb904b44fbd037618f0.png

总结

本文主要通过一个模拟短信发送的示例,演示如何通过spring-plugin来实现策略模式。如果我们对扩展性有要求除了spi,我们也可以考虑使用spring-plugin。不过基于spring-plugin扩展时,要注意具体的插件实现类要为spring的bean,不然插件会找不到

更多详细例子可以查看官网

demo链接

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消