zoukankan      html  css  js  c++  java
  • if else太多怎么代替,太难维护?可以使用spring-plugin 插件系统

    一、springboot +spring-plugin

    二、针对根据入参不同可能有不同实现逻辑的场景写个demo,如:针对支付宝或者微信支付渠道发起的支付请求

    1).引入相关依赖

            <dependency>
                <groupId>org.springframework.plugin</groupId>
                <artifactId>spring-plugin-core</artifactId>
            </dependency>

    2).请求参数实体属性

    @Data
    public class PayRequestVO {
    
        private String channel;
        
        private String phone;
        
        private BigDecimal money;
    }

    支付接口:

    public interface OrderPayOperationService extends Plugin<PayRequestVO> {
    
        public String orderPay(PayRequestVO vo);
    }

    支付宝支付实现:

    @Service
    public class AlipayOrderPayOperationServiceImpl implements OrderPayOperationService {
    
        @Override
        public boolean supports(PayRequestVO delimiter) {
            return "alipay".equalsIgnoreCase(delimiter.getChannel());
        }
    
        @Override
        public String orderPay(PayRequestVO vo) {
            return "支付宝支付";
        }
    
    }

    微信支付实现:

    @Service
    public class WxOrderPayOperationServiceImpl implements OrderPayOperationService {
    
        @Override
        public boolean supports(PayRequestVO delimiter) {
            return false;
        }
    
        @Override
        public String orderPay(PayRequestVO vo) {
            return "微信支付";
        }
    
    }

    将业务接口注入到插件系统:@EnablePluginRegistries({ OrderPayOperationService.class })

    test:

    @SpringBootApplication //Spring Boot核心注解,用于开启自动配置
    @EnablePluginRegistries({ OrderPayOperationService.class })
    @Slf4j
    public class DemoApplication {
        //程序可以直接在此启动
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    
            PluginRegistry<OrderPayOperationService, PayRequestVO> orderPayOperationServicePluginRegistry = (PluginRegistry<OrderPayOperationService, PayRequestVO>) context
                    .getBean("orderPayOperationServiceRegistry");
            PayRequestVO vo = new PayRequestVO();
            vo.setChannel("alipay");
            vo.setPhone("1231231231");
            vo.setMoney(new BigDecimal(100));
            //获取插件
            OrderPayOperationService orderPayOperationService = orderPayOperationServicePluginRegistry.getPluginFor(vo);//getPlugins();
            //发起订单支付
            String orderPay = orderPayOperationService.orderPay(vo);
            log.info("支付返回:{}", orderPay);
        }
    
    }

    运行结果:

     总结:

    1.使用spring-plugin可以方便代码拓展功能,方便维护;

    2.架构清晰,结构分层;

    3.业务实现解耦,保持原有业务稳定性,新增业务无需动到框架层面.

    参考:https://blog.csdn.net/u010192145/article/details/90487058

  • 相关阅读:
    Http方法:Get请求与Post请求的区别
    udev和rules使用规则
    c++中的动态内存分配
    c++中重载运算符
    c++中静态成员函数
    c++中值传递,址传递,引用传递
    c++中实现单例模式singleton class
    [Windows篇] 在windows 10上源码编译gtest 并编写CMakeLists.txt
    [Ubuntu篇] 在ubuntu上源码编译gtest,编写gtest-config.cmake并测试
    使用boost data_time模块来获取毫秒级时间并转换为string字符串
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/13186968.html
Copyright © 2011-2022 走看看