RequestMappingHandlerMapping类详解 - HandlerMapping系列九

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();
}

主要包含了路径匹配策略、@RequestMapping属性匹配策略。

总结

1. RequestMappingHandlerMapping ,用于注解@Controller,@RequestMapping来定义controller。

2. RequestMappingHandlerMapping这个类在程序中用@Autowired来自动装配得到对象,但实际上,spring容器是没有注入到这个bean的,这个可以用applicationContext.getBean("requestMappingHandlerMapping")方法来验证得到。

3. RequestMappingHandlerMapping这个类的对象,只能在controller层用,并且要在申明了@RequestMapping的方法里面用。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。