SpringMVC基本原理

2023-02-07  本文已影响0人  头发掉了

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。

MVC.jpg

核心类 控制器:DispatchServlet, 视图转换器:ViewResolver,拦截器:HandlerInterceptors,ThemeResolver:主题转换器,HandlerAdapter:优先级形式适配器设计模式处理Handler,HandlerExecutionChain:责任链处理Handler,包含所有的HandlerInterceptors。AbstractController:Controller,
MessageConverters:数组资源,xml转换器 MethodFilter:HttpMethod过滤器

下面是MVC执行流程
总体是比较简单的


mvc.png

结合Spring IOC,1->获得内部的HadlerMethod,

 * Return a map with all handler methods and their mappings.
     */
    public Map<T, HandlerMethod> getHandlerMethods() {
        return Collections.unmodifiableMap(this.handlerMethods);
    }

    /**
     * Detects handler methods at initialization.
     */
    public void afterPropertiesSet() {
        initHandlerMethods();
    }

    /**

verride
    protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
               //获取绑定的Path
        String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
        if (logger.isDebugEnabled()) {
            logger.debug("Looking up handler method for path " + lookupPath);
        }
                  //绑定HandlerMethods
        HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);

        if (logger.isDebugEnabled()) {
            if (handlerMethod != null) {
                logger.debug("Returning handler method [" + handlerMethod + "]");
            }
            else {
                logger.debug("Did not find handler method for [" + lookupPath + "]");
            }
        }
               //将Bean对象转换为handlerMethod
        return (handlerMethod != null) ? handlerMethod.createWithResolvedBean() : null;
    }


用到的设计模式:责任链,策略,适配,装饰等。

上一篇 下一篇

猜你喜欢

热点阅读