zoukankan      html  css  js  c++  java
  • 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);
    }





  • 相关阅读:
    centos7查看系统版本,查看机器位数x86-64
    CentOS7下安装mysql5.6修改字符集为utf8并开放端口允许远程访问
    CentOS7下mysql5.6修改默认编码
    使用swagger作为restful api的doc文档生成
    在idea中maven项目jdk编译version总是跳到1.5
    国内maven镜像,快的飞起
    醒悟,珍惜时间,学会利用资源
    重定向Http status code 303 和 302
    jackson简单使用,对象转json,json转对象,json转list
    2017 年度码云新增项目排行榜 TOP 50,为它们打“call”
  • 原文地址:https://www.cnblogs.com/wchxj/p/11784111.html
Copyright © 2011-2022 走看看