职责链模式在开源代码中的应用
发布日期:2021-05-15 17:43:50 浏览次数:10 分类:精选文章

本文共 5404 字,大约阅读时间需要 18 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������Servlet������Filter���

���Servlet 2.3������������Filter���FilterChain������������������������FilterChain���������������������������Filter���������������������doFilter������������������������������������Filter������������������������������������������Filter���FilterChain������������������

public interface Filter {    void init(FilterConfig filterConfig) throws ServletException;    void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;    void destroy();}

FilterChain������������������������������doFilter������������������������Filter������������������������������������������FilterChain������������������������VirtualFilterChain���������������������������Filter������������Filter������

private static class VirtualFilterChain implements FilterChain {    private final FilterChain originalChain;    private final List
additionalFilters; private int currentPosition = 0; public VirtualFilterChain(FilterChain chain, List
additionalFilters) { this.originalChain = chain; this.additionalFilters = additionalFilters; } @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { if (this.currentPosition == this.additionalFilters.size()) { this.originalChain.doFilter(request, response); } else { this.currentPosition++; Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1); nextFilter.doFilter(request, response, this); } }}

������������������������������������������������������Filter���������������������������������������������


������������Spring������������������

���Spring������������HandlerInterceptor���HandlerExecutionChain������������������������������HandlerInterceptor������������������������������

  • preHandle���������������������������������
  • postHandle���������������������������������
  • afterCompletion������������������������������������

HandlerExecutionChain������������������������������������������������������������������HandlerExecutionChain������������������������

public class HandlerExecutionChain {    private HandlerInterceptor[] interceptors;    private int interceptorIndex;    public void applyPreHandle(HttpServletRequest request, HttpServletResponse response)         throws Exception {        if (!this.interceptors Empty(interceptors)) {            for (int i = 0; i < interceptors.length; i++) {                HandlerInterceptor interceptor = interceptors[i];                if (!interceptor.preHandle(request, response, this)) {                    triggerAfterCompletion(request, response, null);                    return false;                }                this.interceptorIndex = i;            }        }        return true;    }    public void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv)         throws Exception {        if (!this.interceptors Empty(interceptors)) {            for (int i = interceptors.length - 1; i >= 0; i--) {                HandlerInterceptor interceptor = interceptors[i];                interceptor.postHandle(request, response, this.handler, mv);            }        }    }    public void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)         throws Exception {        if (!this.interceptors Empty(interceptors)) {            for (int i = this.interceptorIndex; i >= 0; i--) {                HandlerInterceptor interceptor = interceptors[i];                try {                    interceptor.afterCompletion(request, response, this.handler, ex);                } catch (Throwable ex2) {                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);                }            }        }    }}

DispatcherServlet���������HandlerAdapter������������������HandlerExecutionChain���applyPreHandle������������������������������������applyPostHandle������������������������������������applyPreHandle������false������������afterCompletion���������������������������


���Java���������������������������

������������������������Java���������������������

  • ���Effective Java������������������������������Java������������������������
  • ���Java Concurrency in Practice������������������Java������������������
  • ���The Java Collections Library������������������Java���������������
  • ���Clean Architecture���������������������������������������������
  • ��� Design Patterns������������������������������������

  • ���Java���������������������������������

    ������������������������Java������������������������

  • What is the difference between List and Collection?
    • List������������������������������������������������Collection������������������
  • Explain singleton enum in Java.
    • enum���������������������static���������������
  • How to handle thread safety in Java concurrency.
    • ������synchronized������������ lock���������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:《Java与模式》
    下一篇:《设计模式》- GoF

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月19日 07时19分52秒