利用 SpringBoot 责任链模式,让编程事半功倍!-方式二

时间:2024-12-16 15:51:27

第二种实现方式,就更简单了,我们通过注解@Order来指定排序,代替手动方法排序sort(),操作方式如下:

package com.example.reterdardemo.service;

import com.example.reterdardemo.entity.OrderAddContext;
import com.example.reterdardemo.interfaces.OrderHandleIntercept;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author qinxun
 * @date 2024/12/09 11:39
 * @dec 指定注入顺序为1
 **/
@Order(1)
@Component
public class RepeatOrderHandleInterceptService implements OrderHandleIntercept {
    @Override
    public int sort() {
        //用于重复下单的逻辑验证,在执行顺序为1
        return 1;
    }

    @Override
    public OrderAddContext handle(OrderAddContext context) {
        System.out.println("通过seqId,检查客户是否重复下单");
        return context;
    }
}
package com.example.reterdardemo.service;

import com.example.reterdardemo.entity.OrderAddContext;
import com.example.reterdardemo.interfaces.OrderHandleIntercept;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author qinxun
 * @date 2024/12/09 11:40
 * @dec
 **/
@Order(2)
@Component
public class ValidOrderHandleInterceptService implements OrderHandleIntercept {
    @Override
    public int sort() {
        return 2;
    }

    @Override
    public OrderAddContext handle(OrderAddContext context) {
        System.out.println("检查请求参数,是否合法,并且获取客户的银行账户");
        return context;
    }
}
package com.example.reterdardemo.service;

import com.example.reterdardemo.entity.OrderAddContext;
import com.example.reterdardemo.interfaces.OrderHandleIntercept;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author qinxun
 * @date 2024/12/09 11:41
 * @dec
 **/
@Order(3)
@Component
public class BankOrderHandleInterceptService implements OrderHandleIntercept {
    @Override
    public int sort() {
        return 3;
    }

    @Override
    public OrderAddContext handle(OrderAddContext context) {
        System.out.println("检查银行账户是否合法,调用银行系统检查银行账户余额是否满足下单金额");
        return context;
    }
}
package com.example.reterdardemo.service;

import com.example.reterdardemo.entity.OrderAddContext;
import com.example.reterdardemo.interfaces.OrderHandleIntercept;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qinxun
 * @date 2024/12/09 11:41
 * @dec
 **/
@Component
public class OrderHandleChainService {

    @Autowired
    private List<OrderHandleIntercept> handleList;


    /**
     * 执行处理
     *
     * @return
     * @paramcontext
     */
    public OrderAddContext execute(OrderAddContext context) {
        for (OrderHandleIntercept handleIntercept : handleList) {
            context = handleIntercept.handle(context);
        }
        return context;
    }
}

运行单元测试,你会发现结果与上面运行的结果一致,原因Springioc容器,支持通过Map或者List来直接注入对象,省去自己排序。

利用 SpringBoot 责任链模式,让编程事半功倍!_责任链_04