Spring MVC组件源码分析

组件概览

HandlerMapping

根据 request 找到对应的处理器 Handler 和 Interceptors。内部只有一个方法

HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;

HandlerAdapter

Handler 适配器,内部方法如下:

boolean supports(Object handler);//判断是否可以使用某个 Handler
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; //具体使用
long getLastModified(HttpServletRequest request, Object handler);//获取资源上一次修改的时间

HandlerExceptionResolver

根据异常设置 ModelAndView ,再交给 render 方法进行渲染。

ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)

ViewResolver

用来将 String 类型的视图名和 Locale 解析为 View 类型的视图。

View resolveViewName(String viewName, Locale locale) throws Exception;

它的一个实现类 BeanNameViewResolver,它重写 resolveViewName 方法如下:

public View resolveViewName(String viewName, Locale locale) throws BeansException {
        ApplicationContext context = getApplicationContext();
        //如果应用上下文没有找到视图,返回 null
        if (!context.containsBean(viewName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("No matching bean found for view name ‘" + viewName + "‘");
            }
            // Allow for ViewResolver chaining...
            return null;
        }
        //如果找到的视图类型不匹配,也返回 null
        if (!context.isTypeMatch(viewName, View.class)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found matching bean for view name ‘" + viewName +
                        "‘ - to be ignored since it does not implement View");
            }
            // Since we‘re looking into the general ApplicationContext here,
            // let‘s accept this as a non-match and allow for chaining as well...
            return null;
        }
        //根据视图名称从 Spring 容器中查找 Bean,返回找到的 bean
        return context.getBean(viewName, View.class);
    }

RequestToViewNameTranslator

获取 request 中的视图名。接口里面也是只有一个方法:

String getViewName(HttpServletRequest request) throws Exception; //根据 request 查找视图名

LocaleResolver

用于从 request 解析出 Locale。

public interface LocaleResolver {
    //从 request 解析出 Locale
    Locale resolveLocale(HttpServletRequest request);
    //根据 request 设置  locale
    void setLocale(HttpServletRequest request, HttpServletResponse response, @Nullable Locale locale);
}

ThemeResolver

解析主题

public interface ThemeResolver {
    //通过给定的 request 查找主题名
    String resolveThemeName(HttpServletRequest request);
    //根据给定的 request 设置主题名
    void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);
}

在 RequestContext.java 文件中可以获取主题:

public String getThemeMessage(String code, String defaultMessage) {
        //获取主题的信息
        return getTheme().getMessageSource().getMessage(code, null, defaultMessage, this.locale);
    }

public Theme getTheme() {
        //判断主题是否为空
        if (this.theme == null) {
            // 通过 RequestContextUtils 获取 request 中的主题名
            this.theme = RequestContextUtils.getTheme(this.request);
            if (this.theme == null) {   //如果还是为空的话
                //那就是没有有效的主题解析器和主题
                this.theme = getFallbackTheme();
            }
        }
        return this.theme;
    }

RequestContextUtils.getTheme() 方法:

public static Theme getTheme(HttpServletRequest request) {
        ThemeResolver themeResolver = getThemeResolver(request);
        ThemeSource themeSource = getThemeSource(request);
        if (themeResolver != null && themeSource != null) {
            String themeName = themeResolver.resolveThemeName(request);
            return themeSource.getTheme(themeName);
        }
        else {
            return null;
        }
    }

MultipartResolver

用于处理上传请求,处理方法:将普通的 request 包装成 MultipartHttpServletRequest

public interface MultipartResolver {
    //根据 request 判断是否是上传请求
    boolean isMultipart(HttpServletRequest request);
    //将 request 包装成 MultipartHttpServletRequest
    MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
    //清理上传过程中产生的临时资源
    void cleanupMultipart(MultipartHttpServletRequest request);
}

FlashMapManager

FlashMap 主要在 redirect 中传递参数,FlashMapManager 用来管理 FlashMap 的。

public interface FlashMapManager {
    //恢复参数,并将恢复过的和超时的参数从保存介质中删除
    @Nullable
    FlashMap retrieveAndUpdate(HttpServletRequest request, HttpServletResponse response);
    //将参数保存起来
    void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest request, HttpServletResponse response);
}

小结

介绍 Spring MVC 中九大组件的接口、作用、内部方法实现及作用进行了简单的介绍,详细的还需大家自己去看源码。

总结

Spring MVC 原理总结

本质是一个 Servlet,这个 Servlet 继承自 HttpServlet。Spring MVC 中提供了三个层次的 Servlet:HttpServletBean、FrameworkServlet 和 DispatcherServlet。他们相互继承, HttpServletBean 直接继承自 Java 的 HttpServlet。HttpServletBean 用于将 Servlet 中的 Servlet 中配置的参数设置到相应的属性中,FrameworkServlet 初始化了 Spring MVC 中所使用的 WebApplicationContext,具体处理请求的 9 大组件是在 DispatcherServlet 中初始化的,整个继承图如下:

原文地址:https://blog.51cto.com/14230003/2425544

时间: 2024-07-29 14:04:08

Spring MVC组件源码分析的相关文章

Spring mvc之源码 handlerMapping和handlerAdapter分析

Spring mvc之源码 handlerMapping和handlerAdapter分析 本篇并不是具体分析Spring mvc,所以好多细节都是一笔带过,主要是带大家梳理一下整个Spring mvc的执行流程,以及如何根据URL查找处理器Controller的实现 (适合那些刚阅读源码不知道如何下手的人) http://www.guojinbao.com/borrow/borrowDetail/GETadLPjnf0[d].do 如何根据URL地址---->找到正确处理器Controller

Spring Core Container 源码分析三:Spring Beans 初始化流程分析

前言 本文是笔者所著的 Spring Core Container 源码分析系列之一: 本篇文章主要试图梳理出 Spring Beans 的初始化主流程和相关核心代码逻辑: 本文转载自本人的私人博客,伤神的博客: http://www.shangyang.me/2017/04/01/spring-core-container-sourcecode-analysis-beans-instantiating-process/ 本文为作者的原创作品,转载需注明出处: 源码分析环境搭建 参考 Sprin

Spring Core Container 源码分析七:注册 Bean Definitions

前言 原本以为,Spring 通过解析 bean 的配置,生成并注册 bean defintions 的过程不太复杂,比较简单,不用单独开辟一篇博文来讲述:但是当在分析前面两个章节有关 @Autowired.@Component.@Service 注解的注入机制的时候,发现,如果没有对有关 bean defintions 的解析和注册机制彻底弄明白,则很难弄清楚 annotation 在 Spring 容器中的底层运行机制:所以,本篇博文作者将试图去弄清楚 Spring 容器内部是如何去解析 b

Spring IOC 容器源码分析 - 创建单例 bean 的过程

1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去创建,而是从缓存中获取.如果某个 bean 还未实例化,这个时候就无法命中缓存.此时,就要根据 bean 的配置信息去创建这个 bean 了.相较于getBean(String)方法的实现逻辑,创建 bean 的方法createBean(String, RootBeanDefinition, Obj

Spring IOC 容器源码分析 - 余下的初始化工作

1. 简介 本篇文章是"Spring IOC 容器源码分析"系列文章的最后一篇文章,本篇文章所分析的对象是 initializeBean 方法,该方法用于对已完成属性填充的 bean 做最后的初始化工作.相较于之前几篇文章所分析的源码,initializeBean 的源码相对比较简单,大家可以愉快的阅读.好了,其他的不多说了,我们直入主题吧. 2. 源码分析 本章我们来分析一下 initializeBean 方法的源码.在完成分析后,还是像往常一样,把方法的执行流程列出来.好了,看源码

Spring Developer Tools 源码分析:二、类路径监控

在 Spring Developer Tools 源码分析一中介绍了 devtools 提供的文件监控实现,在第二部分中,我们将会使用第一部分提供的目录监控功能,实现对开发环境中 classpath 的监控. 二.类路径监控 首先看一些这一部分可能涉及到的类图: 在图中,红色斜线左上部分是第一部分中介绍的文件目录监控的类,其中 FileSystemWatcher 会通过独立线程监控指定的目录,当目录内容发生变化时,通过对比快照可以获得所有监控目录变化的文件ChangedFiles,然后将变化通知

django的RBAC认证z;自定义auth_user表;认证组件权限组件源码分析;认证组件;权限组件

一 RBAC 1.RBAC:全称(Role-Based Access Control):指的是基于用户权限访问控制的认证. 2.Django框架采用的是RBAC认证规则,RBAC认证规则通常会分为:三表规则,五表规则:Django采用的是六表规则. # 三表:用户表.角色表.权限表# 五表:用户表.角色表.权限表.用户角色关系表.角色权限关系表# 六表:用户表.角色表.权限表.用户角色关系表.角色权限关系表.用户权限关系表 3.在Django中六表之间是都是多对多的关系,可通过下面字段跨表访问

Spring IOC 容器源码分析

前言: Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文并不能让你成为 Spring 专家,不过一定有助于大家理解 Spring 的很多概念,帮助大家排查应用中和 Spring 相关的一些问题. 阅读建议:读者至少需要知道怎么配置 Spring,了解 Spring 中的各种概念,少部分内容我还假设读者使用过 SpringMVC.本文要说的 IOC

深入理解 spring 容器,源码分析加载过程

Spring框架提供了构建Web应用程序的全功能MVC模块,叫Spring MVC,通过Spring Core+Spring MVC即可搭建一套稳定的Java Web项目.本文通过Spring MVC源码分析介绍它的核心实现原理. Tomcat服务器启动入口文件是web.xml,通过在其中配置相关的Listener和Servlet即可加载Spring MVC所需数据.基于Spring MVC最简单的配置如下. <!-- 加载Spring配置文件 --> <context-param>