[Spring MVC]HandlerMapping的初始化

HttpServletBean#init

容器初始化DispatcherServlet这个Servlet实例的时候,会调用其init()方法(该方法在HttpServletBean中),HttpServletBean会执行子类的initServletBean方法,在这里,FrameworkServlet会进行容器的初始化,在容器的refresh执行到finishRefresh的时候,会发布事件,最终会激活FrameworkServlet#onApplicationEvent,最终就会执行到org.springframework.web.servlet.DispatcherServlet#initStrategies中进行MVC组件的初始化.
下面来看IDEA的执行栈:

执行栈

初始化MVC的九大组件

  • org.springframework.web.servlet.DispatcherServlet#initStrategies
protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);
    initLocaleResolver(context);
    initThemeResolver(context);
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
    initFlashMapManager(context);
}
  • MultipartResolver
    MultipartResolver主要用来处理文件上传请求,它会将请求包装成MultipartHttpServletRuest实例,通过它的抽象子类AbstractMultipartHttpServletRequest可以看到很多跟文件相关的方法.例如:
@Override
public Iterator<String> getFileNames() {
    return getMultipartFiles().keySet().iterator();
}

@Override
public MultipartFile getFile(String name) {
    return getMultipartFiles().getFirst(name);
}

@Override
public List<MultipartFile> getFiles(String name) {
    List<MultipartFile> multipartFiles = getMultipartFiles().get(name);
    if (multipartFiles != null) {
        return multipartFiles;
    }
    else {
        return Collections.emptyList();
    }
}

@Override
public Map<String, MultipartFile> getFileMap() {
    return getMultipartFiles().toSingleValueMap();
}
  • LocaleResolver
    视图渲染组件ViewResolverresolveViewName方法需要传输一个Locale实例,这个实例对象由LocaleResovler进行解析。这也是Spring MVC对国际化的支持.

  • ThemeResolver

用于进行主题渲染

  • HandlerMapping
    核心组件,它会将被@RequestMapping注解标记的Controller解析成HandlerMapping实例,在HandMapping中声明了一个getHandler方法,在处理请求的时候,MVC会用这个方法找到匹配的处理方法。
HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
  • HandlerAdapters
    这是一个Handler的适配器,Handler可以以任意的形式存在,但是servlet请求都是以doService(HttpServletRequest req,HttpServletResponse resp)请求的,要让固定的Servlet方法调用Handler进行处理,这就是适配器要做的事情。

  • HandlerExceptionResolvers
    HandlerExceptionResolvers是用来处理Handler产生的异常组件。它会根据异常设置ModelAndView,然后交由渲染器进行渲染。

  • ViewResovler
    渲染视图,Spring MVC最终返回的是View类型的视图。如jsp,就需要使用到.

  • RequestToViewNameTranslator
    RequestToViewNameTranslator的作用是从请求中获取ViewName,因为ViewResovler根据ViewName查找View.如果Handler没有指定返回的ViewName,交由该组件处理.

  • FlashMapManager
    FlashMap用于传递重定向的参数。

HanlderMapping的初始化

用户的请求经过DispatcherServlet会根据HandlerMapping来定位到具体的Controller#method.当容器启动的时候,会对标记了@RqequestMapping@Controller的Bean进行HandlerMapping映射的创建。

初始化RequestMappingHandlerMapping

  • UML
UML

RequestMappingHandlerMapping实现了许多的Aware接口,可以从容器中获取ApplicationContextServletContextBeanNameEmbeddedValueResovler.同时,RequestMappingHandlerMapping继承的AbstractHandlerMethodMapping还实现了IoC的生命周期回调函数InitializingBean.

  • org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#afterPropertiesSet
/**
 * Detects handler methods at initialization.
 * @see #initHandlerMethods
 */
@Override
public void afterPropertiesSet() {
    initHandlerMethods();
}

在IoC进行doCreateBean的时候,会回调InitializingBean#afterPropertiesSet函数.我们进入这个函数看看执行了什么逻辑.

  • org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#initHandlerMethods
/**
 * Scan beans in the ApplicationContext, detect and register handler methods.
 * @see #getCandidateBeanNames()
 * @see #processCandidateBean
 * @see #handlerMethodsInitialized
 */
protected void initHandlerMethods() {
    // 遍历容器中所有的beanName
    for (String beanName : getCandidateBeanNames()) {
        // 如果beanName以“scopedTarget.”开头,忽略
        // 通常这些代理Bean的scope都为(session、application、request)
        if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
            processCandidateBean(beanName);
        }
    }
    handlerMethodsInitialized(getHandlerMethods());
}

从容器中获取所有的beanName集合,遍历beanNames.
对beanName不以"scopedTarget."开头的bean进行处理.

  • org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#processCandidateBean
protected void processCandidateBean(String beanName) {
    Class<?> beanType = null;
    try {
        // 获取bean对应的class对象
        beanType = obtainApplicationContext().getType(beanName);
    }
    catch (Throwable ex) {
        // An unresolvable bean type, probably from a lazy bean - let's ignore it.
        if (logger.isTraceEnabled()) {
            logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
        }
    }
    // 判断class对象上是否有@Controller和@RequestMapping的注解
    if (beanType != null && isHandler(beanType)) {
        // 提取其url与controller映射关系
        detectHandlerMethods(beanName);
    }
}
  1. 通过beanName获取该bean的Type.
  2. 判断class对象上是否有@Controller和@RequestMapping的注解,提取其url与method的映射关系.
  • org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#detectHandlerMethods
    /**
     * Look for handler methods in the specified handler bean.
     * @param handler either a bean name or an actual handler instance
     * @see #getMappingForMethod
     */
    protected void detectHandlerMethods(Object handler) {
        // 如果handler是字符串,证明是一个beanName,则从IoC容器中获取其class对象;
        // 否则直接获取class对象
        Class<?> handlerType = (handler instanceof String ?
                obtainApplicationContext().getType((String) handler) : handler.getClass());

        if (handlerType != null) {
            // 为了确保获取到的类是被代理的类
            Class<?> userType = ClassUtils.getUserClass(handlerType);
            // 寻找方法上有@RequestMapping注解的Method实例
            // 注意这里的methods是一个map,key为method实例,value是RequestMappingHandlerMapping实例
            Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
                    (MethodIntrospector.MetadataLookup<T>) method -> {
                        try {
                            return getMappingForMethod(method, userType);
                        }
                        catch (Throwable ex) {
                            throw new IllegalStateException("Invalid mapping on handler class [" +
                                    userType.getName() + "]: " + method, ex);
                        }
                    });
            if (logger.isTraceEnabled()) {
                logger.trace(formatMappings(userType, methods));
            }
            // 将获取到的Method对象依次注册到HandlerMapping中
            methods.forEach((method, mapping) -> {
                // 获取被代理的方法实例
                Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
                registerHandlerMethod(handler, invocableMethod, mapping);
            });
        }
    }
  1. 获取需要处理的class对象.
  2. 如果当前bean是代理类,需要获取被代理的类,也就是TargetClass.
  3. 通过MethodIntrospector#selectMethods对当前class中的methods进行遍历,寻找方法上有@RequestMapping注解的Method实例,对其进行解析,随后放入methodMap这个容器中.key为method实例,value是RequestMappingInfo实例
  4. methods进行遍历,在前面获取到的被代理类,现在需要转换成代理类的方法实例.随后对当前方法进行注册.
  • org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#getMappingForMethod
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
    // 根据当前方法解析出RequestMappingInfo实例
    RequestMappingInfo info = createRequestMappingInfo(method);
    if (info != null) {
        // 创建类上面的RequestMapping信息
        RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
        if (typeInfo != null) {
            // 将两个信息合并
            info = typeInfo.combine(info);
        }
        String prefix = getPathPrefix(handlerType);
        if (prefix != null) {
            info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info);
        }
    }
    return info;
}
  1. 解析method的注解,构造出RequestMappingInfo实例.
  2. 解析class上的RequestMappingInfo实例.
  3. 将两个RequestMappingInfo进行合并.
  • org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#createRequestMappingInfo(java.lang.reflect.AnnotatedElement)
@Nullable
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
    // 如果该函数含有@RequestMapping注解,则解析该注解的信息
    // 否则返回null
    // 关键方法: createRequestMappingInfo
    RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
    RequestCondition<?> condition = (element instanceof Class ?
            getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element));
    return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
}
  1. 如果该函数含有@RequestMapping注解,则解析该注解的信息
  2. 进入重载的createRequestMappingInfo.
  • org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#createRequestMappingInfo(org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.servlet.mvc.condition.RequestCondition<?>)
protected RequestMappingInfo createRequestMappingInfo(
        RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition) {
    // 使用builder模式进行参数构建
    RequestMappingInfo.Builder builder = RequestMappingInfo
            // 支持SPEL表达式的解析
            // RequestMappingHandlerMapping实现了EmbeddedValueResolverAware接口
            .paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
            .methods(requestMapping.method())
            .params(requestMapping.params())
            .headers(requestMapping.headers())
            .consumes(requestMapping.consumes())
            .produces(requestMapping.produces())
            .mappingName(requestMapping.name());
    if (customCondition != null) {
        builder.customCondition(customCondition);
    }
    return builder.options(this.config).build();
}

注册映射关系

  • org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#register
public void register(T mapping, Object handler, Method method) {
    this.readWriteLock.writeLock().lock();
    try {
        // 创建HandlerMethod实例
        HandlerMethod handlerMethod = createHandlerMethod(handler, method);
        // 验证方法的唯一性,也就是当前方法映射关系是否已经注册过了
        assertUniqueMethodMapping(handlerMethod, mapping);
        // 注册RequestMappingInfo和HandlerMethod
        this.mappingLookup.put(mapping, handlerMethod);
        // 注册请求路径和对应的RequestMappingInfo
        List<String> directUrls = getDirectUrls(mapping);
        for (String url : directUrls) {
            this.urlLookup.add(url, mapping);
        }

        String name = null;
        if (getNamingStrategy() != null) {
            // 注册请求路径和HandlerMethod的映射
            name = getNamingStrategy().getName(handlerMethod, mapping);
            addMappingName(name, handlerMethod);
        }

        CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
        if (corsConfig != null) {
            // 注册HandlerMethod与跨域信息的映射
            this.corsLookup.put(handlerMethod, corsConfig);
        }
        // 创建以及注册MappingRegistration信息
        this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls, name));
    }
    finally {
        this.readWriteLock.writeLock().unlock();
    }
}
  1. 上读写锁,防止并发引发的线程不安全问题
  2. 创建HandlerMethod实例
  3. 验证方法的唯一性,也就是当前方法映射关系是否已经注册过了
  4. 注册RequestMappingInfo和HandlerMethod
  5. 注册请求路径和对应的RequestMappingInfo
  6. 注册HandlerMethod与跨域信息的映射
  7. 解锁
  • org.springframework.web.method.HandlerMethod
public class HandlerMethod {

    /** Logger that is available to subclasses. */
    protected final Log logger = LogFactory.getLog(getClass());

    private final Object bean;

    @Nullable
    private final BeanFactory beanFactory;
    /**
     * Controller类型
     */
    private final Class<?> beanType;
    /**
     * 方法实例
     */
    private final Method method;

    private final Method bridgedMethod;
    /**
     * 方法参数数组
     */
    private final MethodParameter[] parameters;

    @Nullable
    private HttpStatus responseStatus;

    @Nullable
    private String responseStatusReason;

    @Nullable
    private HandlerMethod resolvedFromHandlerMethod;

    @Nullable
    private volatile List<Annotation[][]> interfaceParameterAnnotations;
}

DispatcherServlet对HandleMapping的初始化

  • org.springframework.web.servlet.DispatcherServlet#initHandlerMappings
private void initHandlerMappings(ApplicationContext context) {
    this.handlerMappings = null;
    // 是否检查所有的HandlerMapping实现类并加载,默认为true
    if (this.detectAllHandlerMappings) {
        // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
        // 寻找IoC容器中HandlerMapping类型的Bean实例
        Map<String, HandlerMapping> matchingBeans =
                BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
        if (!matchingBeans.isEmpty()) {
            this.handlerMappings = new ArrayList<>(matchingBeans.values());
            // We keep HandlerMappings in sorted order.
            // 对HandlerMapping列表进行排序
            AnnotationAwareOrderComparator.sort(this.handlerMappings);
        }
    }
    else {
        try {
            // 从容器中获取beanName为handlerMapping的Bean
            HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
            this.handlerMappings = Collections.singletonList(hm);
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Ignore, we'll add a default HandlerMapping later.
        }
    }

    // Ensure we have at least one HandlerMapping, by registering
    // a default HandlerMapping if no other mappings are found.
    if (this.handlerMappings == null) {
        this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
        if (logger.isTraceEnabled()) {
            logger.trace("No HandlerMappings declared for servlet '" + getServletName() +
                    "': using default strategies from DispatcherServlet.properties");
        }
    }
}
  1. 这里会从容器中找到实现了HandlerMapping接口的bean.进行排序后赋值给handlerMappings变量.
    RequestMappingHandlerMapping实现了这个接口,并进行了初始化注册进了容器中.
    因此,此处就建立了DispatcherServletRequestMappingHandlerMapping之间的联系.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272