责任链模式解析FilterChain

news/2025/2/19 0:04:06/

分析:
过滤器链的调用过程
A过滤器调用chain.doFilter此时会进入到下一个过滤器
B过滤器继续调用chain.doFilter会继续进入下一个过滤器
当所有过滤器都执行完成后,会进入目标方法。
既然chain.doFilter能进入下一个过滤器,那本质上就是方法的调用压栈和弹栈,一个方法的调用入口会执行完成所有的方法栈后才完成。
所以chain内肯定知道下一个filter是哪个,同时chain肯定保存了最终的目标执行对象。
基于以上分析,chain内部维护了一个过滤器集合,并知道下一个过滤器是哪个。

运行结果:首先,任何编程语言在调用一个方法,都会层层调用多层方法栈。ABCDE....其实,责任链设计模式无非就是通过代码解耦,将复杂的逻辑线性化。将一个一个的调用串行化。FilterChain将责任链的执行权交给每个过滤器,由过滤器决定是否执行下一个过滤器。
@Data
@Builder
public class HttpServletRequest {private String method;private String uri;}
public class TargetController {public void target() {System.out.println("执行了最终的目标方法");}}
public interface Filter {void doFilter(FilterChain filterChain, HttpServletRequest request) throws Exception;}
public class AuthFilter implements Filter {@Overridepublic void doFilter(FilterChain filterChain, HttpServletRequest request) throws Exception {System.out.println("auth filter before");filterChain.doFilter(request);System.out.println("auth filter after");}}
public class LogFilter  implements Filter{@Overridepublic void doFilter(FilterChain filterChain, HttpServletRequest request) throws Exception {System.out.println("log filter before ");filterChain.doFilter(request);System.out.println("log filter after ");}
}
@Data
@Builder
public class FilterChain {private Integer index;private List<Filter> filters;//执行最终目标方法的对象private Object target;//执行最终的目标方法private Method method;//执行最终目标方法的private Object[] args;public void doFilter(HttpServletRequest request) throws Exception {//获取当前要执行的过滤器if (index == null){index = 0;}if (filters == null || filters.size() <= 0){return;}if (index >= filters.size()){//执行最终的目标方法method.invoke(target,args);return;}//获取过滤器Filter filter = filters.get(index++);filter.doFilter(this,request);}}
public class Test {public static void main(String[] args) throws Exception {FilterChain filterChain = FilterChain.builder().filters(Arrays.asList(new AuthFilter(), new LogFilter())).index(0).target(new TargetController()).method(TargetController.class.getMethod("target")).args(null).build();filterChain.doFilter(new HttpServletRequest("GET","/test"));}}

http://www.ppmy.cn/news/1573217.html

相关文章

正则化(Regularization)和正则表达式(Regular Expression)区别

文章目录 1. **正则化&#xff08;Regularization&#xff09;**2. **正则表达式&#xff08;Regular Expression&#xff09;**关键区别为什么名字相近&#xff1f; 正则化&#xff08;Regularization&#xff09;和正则表达式&#xff08;Regular Expression&#xff09;不是…

HUP-3D:用于辅助自我中心手持超声探头姿态估计的3D多视角合成数据集

文章目录 HUP-3D: A 3D Multi-view Synthetic Dataset for Assisted-Egocentric Hand-Ultrasound-Probe Pose Estimation摘要方法 HUP-3D: A 3D Multi-view Synthetic Dataset for Assisted-Egocentric Hand-Ultrasound-Probe Pose Estimation 摘要 背景&#xff1a; 以自我为…

日常问题-pnpm install执行没有node_modules生成

日常问题-pnpm install执行没有node_modules生成 1.问题2.解决方法 1.问题 执行pnpm i后&#xff0c;提示Scope: all 3 workspace projects Done in 503ms&#xff0c;而且没有node_modules生成。很奇怪 2.解决方法 确保根目录有 pnpm-workspace.yaml 文件&#xff1a; 把这…

web第三次作业

实现网页登入窗口的显示隐藏 <!DOCTYPE html> <html lang"zh"> <head> <meta charset"UTF-8"> <meta name"viewport" content"widthdevice-width, initial-scale1.0"> <title>登录窗口滑动效果…

第十篇:电源设计的“能量矩阵”——无线充电与碳化硅LLC谐振

副标题 &#xff1a;用SiC器件重构《黑客帝国》的能源母体&#xff0c;打造11kW无线充电的“代码圣殿” ▶ 隐喻框架——能量矩阵的数字化生存 - 核心设定 &#xff1a; - 能量母体 整车电源系统&#xff08;OBC/DCDC/无线充电&#xff09; - 史密斯病毒 电磁干…

Deep seek学习日记1

Deepseek最强大的就是它的深度思考&#xff0c;并且展现了它的思考过程。 五种可使用Deep seek的方式&#xff08;应该不限于这五种&#xff0c;后续嵌入deepseek的应该更多&#xff0c;多了解一点因为官网容易崩~~&#xff09;&#xff1a; 1.deep seek官网 2.硅基流动silicon…

Python使用Flask结合DeepSeek开发

一、背景 我之前关于DeepSeek使用ollama部署的文章大家可以把DeepSeek大模型部署起来。那么ollama还提供了可以调用对应部署模型的API接口。我们可以基于这些接口&#xff0c;做自己的二次开发。使用pythonflaskollama就可以进行模型对话调用。并且前端采用SSE的技术&#xff0…

使用PHP爬虫获取1688商品分类:实战案例指南

在电商领域&#xff0c;商品分类信息是商家进行市场调研、选品分析和竞争情报收集的重要基础。1688作为国内领先的B2B电商平台&#xff0c;提供了丰富且详细的商品分类数据。通过PHP爬虫技术&#xff0c;我们可以高效地获取这些分类信息&#xff0c;为商业决策提供有力支持。 …