RequestMappingHandlerMapping类的简介说明

java-教程王 SpringMVC 发布时间:2021-12-16 10:20:24 阅读数:4881 1
下文笔者将讲述RequestMappingHandlerMapping类的简介说明,如下所示:
RequestMappingHandlerMapping的功能:
   RequestMappingHandlerMapping是AbstractHandlerMethodMapping唯一的实现类
     它根据@RequestMapping注解生成 RequestMappingInfo,同时提供isHandler实现。

afterPropertiesSet() 重写父类初始化方法

@Override
public void afterPropertiesSet() {
	//config为RequestMappingInfo的创建对象
	this.config = new RequestMappingInfo.BuilderConfiguration();
	//设置urlPathHelper默认为UrlPathHelper.class
	this.config.setUrlPathHelper(getUrlPathHelper());
	//默认为AntPathMatcher,路径匹配校验器
	this.config.setPathMatcher(getPathMatcher());
	//是否支持后缀补充,默认为true
	this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
	//是否添加"/"后缀,默认为true
	this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
	//是否采用mediaType匹配模式,比如.json/.xml模式的匹配,默认为false      
	this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
	//mediaType处理类      
	this.config.setContentNegotiationManager(getContentNegotiationManager());
	//调用父类进行HandlerMethod的注册工作
	super.afterPropertiesSet();
}

isHandler() 判断获取何种类型的Handler

@Override
protected boolean isHandler(Class beanType) {
	//优先匹配@Controller
	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}

获取@Controller/@RequestMapping注解下的handler

getMappingForMethod() 获取HandlerMethod对应的mapping属性
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
	//对method以及class类都进行创建RequestMappingInfo 
	//因为@RequestMapping可以在方法上/类上应用注解
	RequestMappingInfo info = null;
	// 读取方法上的RequestMapping注解信息
	RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
	if (methodAnnotation != null) {
	 // 读取自定义的条件,这边没有使用
	 RequestCondition methodCondition = getCustomMethodCondition(method);
	 // 根据方法上的RequsetMapping注解和自定义条件,生成匹配条件.这边的匹配条件包括http method,request parameter,request header等
	 info = createRequestMappingInfo(methodAnnotation, methodCondition);
	 // 读取类上的RequestMapping注解信息
	 RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
	 if (typeAnnotation != null) {
		 RequestCondition typeCondition = getCustomTypeCondition(handlerType);
		 // 生成类上的匹配条件,并合并方法上的
		 info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
	 }
	}
	return info;
}
protected RequestMappingInfo createRequestMappingInfo(RequestMapping requestMapping, RequestCondition customCondition) {
	return RequestMappingInfo
			.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
			.methods(requestMapping.method())
			.params(requestMapping.params())
			.headers(requestMapping.headers())
			.consumes(requestMapping.consumes())
			.produces(requestMapping.produces())
			.mappingName(requestMapping.name())
			.customCondition(customCondition)
			.options(this.config)
			.build();
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringMVC/202112/2098.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者