美文网首页
springmvc源码分析-HandlerMapping初始化流

springmvc源码分析-HandlerMapping初始化流

作者: hello_kd | 来源:发表于2021-04-18 21:33 被阅读0次

从本文开始,来分析下springmvc几个重要组件是如何工作的。

首选,来看下HandlerMapping

HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;

这个接口主要有这个方法,通过request获取一个HandlerExecutionChain对象,这个对象就是包含了两个成员变量

//handler就是应用程序中定义的controller
    private final Object handler;
//拦截器,在controller方法的执行前后加入自己的逻辑,类似于切面
    private final List<HandlerInterceptor> interceptorList = new ArrayList<>();

简单的说,HandlerMapping就是通过http请求查询到对应controller和interceptor。

springmvc默认提供的实现类有三个,分别为RequestMappingHandlerMapping、BeanNameUrlHandlerMapping、RouterFunctionMapping。
日常开发中用的最多的便是RequestMappingHandlerMapping,因此本文主要来看下这个这个bean对象的初始化流程

初始化流程便是定义在WebMvcConfigurationSupport类中

@Bean
@SuppressWarnings("deprecation")
public RequestMappingHandlerMapping requestMappingHandlerMapping(
        @Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
        @Qualifier("mvcConversionService") FormattingConversionService conversionService,
        @Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {

    RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
    mapping.setOrder(0);
    mapping.setInterceptors(getInterceptors(conversionService, resourceUrlProvider));
    mapping.setContentNegotiationManager(contentNegotiationManager);
    mapping.setCorsConfigurations(getCorsConfigurations());

    PathMatchConfigurer pathConfig = getPathMatchConfigurer();
    if (pathConfig.getPatternParser() != null) {
        mapping.setPatternParser(pathConfig.getPatternParser());
    }
    else {
        mapping.setUrlPathHelper(pathConfig.getUrlPathHelperOrDefault());
        mapping.setPathMatcher(pathConfig.getPathMatcherOrDefault());

        Boolean useSuffixPatternMatch = pathConfig.isUseSuffixPatternMatch();
        if (useSuffixPatternMatch != null) {
            mapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
        }
        Boolean useRegisteredSuffixPatternMatch = pathConfig.isUseRegisteredSuffixPatternMatch();
        if (useRegisteredSuffixPatternMatch != null) {
            mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch);
        }
    }
    Boolean useTrailingSlashMatch = pathConfig.isUseTrailingSlashMatch();
    if (useTrailingSlashMatch != null) {
        mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
    }
    if (pathConfig.getPathPrefixes() != null) {
        mapping.setPathPrefixes(pathConfig.getPathPrefixes());
    }

    return mapping;
}

首先就是设置order,值越小,越先执行。程序中可能存在多个HandlerMapping实现类,因此执行的先后顺序是可以指定的。
然后就是可以添加拦截器interceptor

protected final Object[] getInterceptors(
            FormattingConversionService mvcConversionService,
            ResourceUrlProvider mvcResourceUrlProvider) {

    if (this.interceptors == null) {
        InterceptorRegistry registry = new InterceptorRegistry();
        addInterceptors(registry);
        registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService));
        registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider));
        this.interceptors = registry.getInterceptors();
    }
    return this.interceptors.toArray();
}

springmvc默认会添加两个拦截器ConversionServiceExposingInterceptor和ResourceUrlProviderExposingInterceptor,这里有一个很重要的方法addInterceptors,这方法就是给子类继承重写,让应用程序可以手动添加interceptor

每个拦截器是一个InterceptorRegistration对象,包含了三个成员变量

//拦截器对象
    private final HandlerInterceptor interceptor;
//符合拦截器执行的路径模式
    private final List<String> includePatterns = new ArrayList<>();
//符合拦截器执行的路径模式
    private final List<String> excludePatterns = new ArrayList<>();
//路径匹配器
    @Nullable
    private PathMatcher pathMatcher;
//拦截器的执行顺序
    private int order = 0;

HandlerMapping除了添加拦截器外,还有一个比较重要的方法,是添加跨域信息相关的,同样有个方法addCorsMappings可以让子类重写,由应用程序添加跨域相关的配置。

HandlerMapping其他属性由于应用程序一般不需要去修改,因此这里不再讲述了,当生成RequestMappingHandlerMapping对象后,由于这个类还实现了InitializingBean接口,因此还会执行初始化方法afterPropertiesSet

    public void afterPropertiesSet() {
        this.config = new RequestMappingInfo.BuilderConfiguration();
        this.config.setUrlPathHelper(getUrlPathHelper());
        this.config.setPathMatcher(getPathMatcher());
        this.config.setSuffixPatternMatch(useSuffixPatternMatch());
        this.config.setTrailingSlashMatch(useTrailingSlashMatch());
        this.config.setRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch());
        this.config.setContentNegotiationManager(getContentNegotiationManager());

        super.afterPropertiesSet();
    }

可以看到这个方法主要是将HandlerMapping的相关属性值赋值给内部成员RequestMappingInfo.BuilderConfiguration的属性,然后再调用initHandlerMethods方法

protected void initHandlerMethods() {
    for (String beanName : getCandidateBeanNames()) {
        if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
            processCandidateBean(beanName);
        }
    }
}

这个方法中,循环处理容器的beanName列表,然后取出beanName对应的Class对象,判断该class对象是否为Handler,判断标准就是看类是否有注解了Controller或者RequestMapping,若是Handler,则执行detectHandlerMethods

protected void detectHandlerMethods(Object handler) {
    Class<?> handlerType = (handler instanceof String ?
            obtainApplicationContext().getType((String) handler) : handler.getClass());

    if (handlerType != null) {
        Class<?> userType = ClassUtils.getUserClass(handlerType);
        Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
                (MethodIntrospector.MetadataLookup<T>) method -> {
                    try {
//根据Method对象,获取对应的RequestMappingInfo对象
                        return getMappingForMethod(method, userType);
                    }
                    catch (Throwable ex) {
                        throw new IllegalStateException("Invalid mapping on handler class [" +
                                userType.getName() + "]: " + method, ex);
                    }
                });
        //....中间日志省略
        methods.forEach((method, mapping) -> {
            Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
            registerHandlerMethod(handler, invocableMethod, mapping);
        });
    }
}

这个方法主要分为两个步骤

  1. MethodIntrospector.selectMethods 获取handler(controller)下Method对象及其对应的RequestMappingInfo对象,其中RequestMappingInfo对象就是对方法注解@RequestMapping相关属性值的封装(也会结合注解在类上面的@RequestMapping)
  2. 将Method对象,RequestMappingInfo对象维护到HandlerMapping中,当然还有包含url相关的,这样后续请求到来,就可根据url映射出对应的handler。

上述步骤1涉及到的细节很多,这里不展开了,主要来看下步骤2,registerHandlerMethod的处理

public void register(T mapping, Object handler, Method method) {
            this.readWriteLock.writeLock().lock();
            try {
                HandlerMethod handlerMethod = createHandlerMethod(handler, method);
                validateMethodMapping(handlerMethod, mapping);
                this.mappingLookup.put(mapping, handlerMethod);

                List<String> directUrls = getDirectUrls(mapping);
                for (String url : directUrls) {
                    this.urlLookup.add(url, mapping);
                }

                String name = null;
                if (getNamingStrategy() != null) {
                    name = getNamingStrategy().getName(handlerMethod, mapping);
                    addMappingName(name, handlerMethod);
                }

                CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
                if (corsConfig != null) {
                    this.corsLookup.put(handlerMethod, corsConfig);
                }

                this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls, name));
            }
            finally {
                this.readWriteLock.writeLock().unlock();
            }
        }

这方法主要是对HandlerMapping内部一些map的维护

//RequestMappingInfo,MappingRegistration
        private final Map<T, MappingRegistration<T>> registry = new HashMap<>();
//RequestMappingInfo, HandlerMethod
        private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<>();
//url, RequestMappingInfo列表
        private final MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>();
//name, HandlerMethod
        private final Map<String, List<HandlerMethod>> nameLookup = new ConcurrentHashMap<>();
//cors
        private final Map<HandlerMethod, CorsConfiguration> corsLookup = new ConcurrentHashMap<>();

通过这些map,程序可以快速的根据请求url定位到具体的handler,再执行对应的方法。这个请求的流程后续再讲述。

总结,RequestMapping实例化主要是对内部重要的属性进行赋值,方便应用程序可根据http请求的相关信息,比如url,header头等等,找到对应的Controller,并执行相应的方法。

相关文章

网友评论

      本文标题:springmvc源码分析-HandlerMapping初始化流

      本文链接:https://www.haomeiwen.com/subject/remmlltx.html