• spring mvc 源码简要分析


    关于web项目,运用比较多的是过滤器和拦截器

    过滤器基于责任链设计模式

    创建过滤器链

    / Create the filter chain for this request
    ApplicationFilterChain filterChain =
    ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);

    //调用
    private void internalDoFilter(ServletRequest request,
    ServletResponse response)
    throws IOException, ServletException {

    // Call the next filter if there is one
    if (pos < n) {
    ApplicationFilterConfig filterConfig = filters[pos++];
    try {
    Filter filter = filterConfig.getFilter();

    if (request.isAsyncSupported() && "false".equalsIgnoreCase(
    filterConfig.getFilterDef().getAsyncSupported())) {
    request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
    }
    if( Globals.IS_SECURITY_ENABLED ) {
    final ServletRequest req = request;
    final ServletResponse res = response;
    Principal principal =
    ((HttpServletRequest) req).getUserPrincipal();

    Object[] args = new Object[]{req, res, this};
    SecurityUtil.doAsPrivilege ("doFilter", filter, classType, args, principal);
    } else {
    filter.doFilter(request, response, this);
    }
    } catch (IOException | ServletException | RuntimeException e) {
    throw e;
    } catch (Throwable e) {
    e = ExceptionUtils.unwrapInvocationTargetException(e);
    ExceptionUtils.handleThrowable(e);
    throw new ServletException(sm.getString("filterChain.filter"), e);
    }
    return;
    }

    // We fell off the end of the chain -- call the servlet instance
    try {
    if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
    lastServicedRequest.set(request);
    lastServicedResponse.set(response);
    }

    if (request.isAsyncSupported() && !servletSupportsAsync) {
    request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,
    Boolean.FALSE);
    }
    // Use potentially wrapped request from this point
    if ((request instanceof HttpServletRequest) &&
    (response instanceof HttpServletResponse) &&
    Globals.IS_SECURITY_ENABLED ) {
    final ServletRequest req = request;
    final ServletResponse res = response;
    Principal principal =
    ((HttpServletRequest) req).getUserPrincipal();
    Object[] args = new Object[]{req, res};
    SecurityUtil.doAsPrivilege("service",
    servlet,
    classTypeUsedInService,
    args,
    principal);
    } else {
    servlet.service(request, response);
    }
    } catch (IOException | ServletException | RuntimeException e) {
    throw e;
    } catch (Throwable e) {
    e = ExceptionUtils.unwrapInvocationTargetException(e);
    ExceptionUtils.handleThrowable(e);
    throw new ServletException(sm.getString("filterChain.servlet"), e);
    } finally {
    if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
    lastServicedRequest.set(null);
    lastServicedResponse.set(null);
    }
    }



    拦截器基于动态代理 
    // 执行拦截器的prehandle
    if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    return;
    }


    // Actually invoke the handler.
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

    // 执行拦截器的PostHandle
    mappedHandler.applyPostHandle(processedRequest, response, mv);

    // 执行拦截器的AfterCompletion
    if (mappedHandler != null) {
    mappedHandler.triggerAfterCompletion(request, response, null);
    }





  • 相关阅读:
    main方法为什么一定是Public static void
    DOS下编译运行小应用程序
    HelloWorld
    MySQL INFORMATION_SCHEMA 使用(转)
    MySQL int(M)的意义(转)
    Mysql 字符串类型及大小写
    使用Zookeeper 实现选主从或者分布式锁
    记一次CountDownLatch引发的问题
    Mysql恢复部分数据
    记一次Java内存性能分析
  • 原文地址:https://www.cnblogs.com/wchxj/p/11784111.html
Copyright © 2020-2023  润新知