重构DispatcherServlet模块

package com.jt.dispatcherservlet;

import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.jupiter.api.Test;

class MyMappedInterceptor implements MyHandlerInterceptor{
    boolean before;
    public MyMappedInterceptor(boolean before){
        this.before=before;
    }
    @Override
    public boolean beforeMethod() {
        System.out.println("前置方法执行了");
        return before;
    }
}

interface MyHandlerInterceptor {

    default boolean beforeMethod() {
        return true;
    }

    default void afterMethod() {
        System.out.println("后置方法执行了");
    }
}

interface MyAbstractHandler {
    void invokeMethod();
}

class MyHandler implements MyAbstractHandler {
    String path;
    String msg;

    MyHandler(String path, String msg) {
        this.path = path;
        this.msg = msg;
    }

    void method() {
        System.out.println("我是method方法" + msg);
    }

    ;

    @Override
    public void invokeMethod() {
        method();
    }
}

interface MyHandlerMapping {
    MyHandlerExecutionChain getHandler(MyHttpServletRequest myHttpServletRequest);
}

class MyRequestMappingHandlerMapping extends MyAbstractHandlerMapping {
}

abstract class MyAbstractHandlerMapping implements MyHandlerMapping {
    private final List<MyHandlerInterceptor> adaptedInterceptors = new ArrayList();

    public MyHandlerExecutionChain getHandler(MyHttpServletRequest myHttpServletRequest) {
        //模拟假数据
        MyAbstractHandler myhandler = new MyHandler("/resouce", "hello");
        MyHandlerExecutionChain chain = new MyHandlerExecutionChain(myhandler);
        Iterator<MyHandlerInterceptor> it = adaptedInterceptors.iterator();
        while (it.hasNext()) {
            chain.addInterceptor(it.next());
        }
        return chain;
    }

    protected void initInterceptors() {
        this.adaptedInterceptors.add(new MyMappedInterceptor(false));
    }
}

class MyHandlerExecutionChain {
    private List<MyHandlerInterceptor> interceptorList = new ArrayList<>();
    private final MyAbstractHandler handler;

    public MyHandlerExecutionChain(MyAbstractHandler handler) {
        this.handler = handler;
    }

    public void addInterceptor(MyHandlerInterceptor myInterceptor) {
        this.interceptorList.add(myInterceptor);
    }

    MyAbstractHandler getHandler() {
        return this.handler;
    }

    public boolean applyPreHandle() {
        boolean flag = true;
        Iterator var2 = this.interceptorList.iterator();

        while (var2.hasNext()) {
            boolean result = ((MyHandlerInterceptor) var2.next()).beforeMethod();
            flag = flag && result;
            if (!flag)
                return false;
        }
        return flag;
    }

    public void applyPostHandle() {
        Iterator var2 = this.interceptorList.iterator();

        while (var2.hasNext()) {
            ((MyHandlerInterceptor) var2.next()).afterMethod();
        }
    }
}

class MyHttpServletRequest {
    private String lookupPath;
}

class MyDispatcherServlet {
    private List<MyHandlerMapping> myhandlerMappings = new ArrayList<>();

    public void initHandlerMappings() {
        //虚假数据
        MyRequestMappingHandlerMapping myRequestMappingHandlerMapping = new MyRequestMappingHandlerMapping();
        myRequestMappingHandlerMapping.initInterceptors();
        this.myhandlerMappings.add(myRequestMappingHandlerMapping);
    }

    public MyHandlerExecutionChain getHandler(MyHttpServletRequest myHttpServletRequest) {
        //遍历myhandlerMappings
        Iterator var2 = this.myhandlerMappings.iterator();

        while (var2.hasNext()) {
            MyHandlerMapping mapping = (MyHandlerMapping) var2.next();
            MyHandlerExecutionChain handler = mapping.getHandler(myHttpServletRequest);
            if (handler != null) {
                return handler;
            }
        }
        return null;
    }

    void doDispatch(MyHttpServletRequest myHttpServletRequest) {
        MyHandlerExecutionChain myHandlerExecutionChain = null;
        myHandlerExecutionChain = this.getHandler(myHttpServletRequest);
        if (!myHandlerExecutionChain.applyPreHandle()) {
            return;
        }
        myHandlerExecutionChain.getHandler().invokeMethod();
        myHandlerExecutionChain.applyPostHandle();
    }
}

@SpringBootTest
public class DispatcherServletTest {
    @Test
    public void method() {
        MyDispatcherServlet myDispatcherServlet = new MyDispatcherServlet();
        myDispatcherServlet.initHandlerMappings();
        myDispatcherServlet.doDispatch(new MyHttpServletRequest());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_34116044

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值