玄星幻月006-spring使用责任链模式

基于Spring的责任链模式

Spring环境参考:https://blog.youkuaiyun.com/zhi_zixing/article/details/133487881https://blog.youkuaiyun.com/zhi_zixing/article/details/133487881

代码

在这里插入图片描述

Context

package ai.zixing.vo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

@Getter
@Setter
@ToString
@Accessors(chain = true)
public class Context {
    /**
     * ID
     */
    private Integer id;

    /**
     * 上下文信息
     */
    private String contextInfo = "context";
}

HandleIntercept

package ai.zixing.filter;

import ai.zixing.vo.Context;

public interface HandleIntercept {
    /**
     * 业务逻辑
     *
     * @param context context
     * @return Context
     */
    Context handle(Context context);
}

AHandleInterceptService

package ai.zixing.filter;

import ai.zixing.vo.Context;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(10)
@Component
public class AHandleInterceptService implements HandleIntercept {
    @Override
    public Context handle(Context context) {
        context.setContextInfo(context.getContextInfo() + "_A");
        System.out.println(context);
        return context;
    }
}

BHandleInterceptService

package ai.zixing.filter;

import ai.zixing.vo.Context;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(20)
@Component
public class BHandleInterceptService implements HandleIntercept {
    @Override
    public Context handle(Context context) {
        context.setContextInfo(context.getContextInfo() + "_B");
        System.out.println(context);
        return context;
    }
}

CHandleInterceptService

package ai.zixing.filter;

import ai.zixing.vo.Context;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(30)
@Component
public class CHandleInterceptService implements HandleIntercept {
    @Override
    public Context handle(Context context) {
        context.setContextInfo(context.getContextInfo() + "_C");
        System.out.println(context);
        return context;
    }
}

HandleChainService

package ai.zixing.service;

import ai.zixing.filter.HandleIntercept;
import ai.zixing.vo.Context;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 获取IOC容器
 */
@Component
public class HandleChainService {

    @Resource
    private List<HandleIntercept> handleList;

    /**
     * 执行处理
     */
    public Context execute(Context context, int chainBegin) {
        for (int i = chainBegin - 1; i < handleList.size(); i++) {
            context = handleList.get(i).handle(context);
        }
//        for (HandleIntercept handleIntercept : handleList) {
//            context = handleIntercept.handle(context);
//        }
        return context;
    }
}

ChainController

package ai.zixing.controller;

import ai.zixing.vo.Context;
import ai.zixing.service.HandleChainService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ChainController {

    @Resource
    private HandleChainService orderHandleChainService;

    @GetMapping("/test")
    public void explainPlan(@RequestParam("chainIndex") int chainIndex) {
        orderHandleChainService.execute(new Context(), chainIndex);
    }
}

测试接口 GET http://localhost:8080/test?chainIndex=1
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值