1.网关执行流程分析
2.执行链设计
执行链设计(所有项目中的执行(Execution)链(Chain)都是预先设计好的),这里我们初步定义执行链
/**
* 执行链设计(所有项目中的执行(Execution)链(Chain)都是预先设计好的)
*/
class ExecutionChain{
//拦截器是边缘业务
private List<Interceptor> interceptors;
//处理器是核心业务
private Handler handler;
public ExecutionChain(List<Interceptor> interceptors, Handler handler) {
this.interceptors = interceptors;
this.handler = handler;
}
//执行业务逻辑
public void execute(){
//proceed()
//1.执行拦截器(Interceptor)中的doPre方法
for(int i=0;i<interceptors.size();i++)
interceptors.get(i).doPre();
//2.执行处理器(Handler)中的doHandle方法
handler.doHandle();
//3.执行拦截器(Interceptor)中的doPost方法
for(int i=interceptors.size()-1;i>=0;i--)
interceptors.get(i).doPost();
//先进后出原理
}
}
3. 拦截器入门
package com.jt.interceptor;
import java.util.ArrayList;
import java.util.List;
/**拦截器*/
interface Interceptor{
boolean doPre();
void