首页 技术 正文
技术 2022年11月20日
0 收藏 826 点赞 2,437 浏览 5051 个字

承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler

类结构瞧一瞧

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean{}

此为抽象方法,并实现了initializingBean接口,其实主要的注册操作则是通过afterPropertiesSet()接口方法来调用的

AbstractHandlerMethodMapping#afterPropertiesSet()-初始化HandlerMethod对象

源码奉上

@Override
public void afterPropertiesSet() {
initHandlerMethods();
}

转而看initHandlerMethods(),观察是如何实现加载HandlerMethod


protected void initHandlerMethods() {
//获取springmvc上下文的所有注册的bean
String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));for (String beanName : beanNames) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
Class<?> beanType = null;
try {
beanType = getApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
}
}
//isHandler()是抽象方法,主要供子类需要扫描什么类型的bean
if (beanType != null && isHandler(beanType)) {
//解析其中的HandlerMethod进行注册
detectHandlerMethods(beanName);
}
}
}
//抽象方法,目前尚无实现
handlerMethodsInitialized(getHandlerMethods());
}

接下来稍微分析springmvc是如何解析bean并获取其中的HandlerMethod

AbstractHandlerMethodMapping#detectHandlerMethods()解析

源码奉上

protected void detectHandlerMethods(final Object handler) {
Class<?> handlerType = (handler instanceof String ?
getApplicationContext().getType((String) handler) : handler.getClass());
//因为有些是CGLIB代理生成的,获取真实的类
final Class<?> userType = ClassUtils.getUserClass(handlerType);Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
new MethodIntrospector.MetadataLookup<T>() {
@Override
public T inspect(Method method) {
try {
//模板方法获取handlerMethod的mapping属性
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
}
});//对查找到的HandlerMethod进行注册,保存至内部类mappingRegistry对象中
for (Map.Entry<Method, T> entry : methods.entrySet()) {
//作下判断,method是否从属于userType
Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
T mapping = entry.getValue();
registerHandlerMethod(handler, invocableMethod, mapping);
}
}

针对唯一的实现类RequestMappingHandlerMapping,上述的mapping属性指代的是RequestMappingInfo对象,内部包含@RequestMapping注解的内部属性,比如methodparamsconsumesproducesvalue以及对应的属性判断类

RequestMappingHandlerMapping-唯一实现类

作为HandlerMethod对象的配置者,我们主要观察其复写的几个方法

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

此处如何设置可通过查看博文>>>SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

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

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

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

RequestMappingHandlerMapping#getMappingForMethod()-获取HandlerMethod对应的mapping属性

此处指的是RequestMappingInfo,主要包含了路径匹配策略、@RequestMapping属性匹配策略,简单源码奉上

@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
//对method以及class类都进行创建RequestMappingInfo
//因为@RequestMapping可以在方法上/类上应用注解
RequestMappingInfo info = createRequestMappingInfo(method);
if (info != null) {
RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
if (typeInfo != null) {
info = typeInfo.combine(info);
}
}
return info;
}

由代码可知,其将拼装Class上的@RequestMapping和Method上的@RequestMapping组装成RequestMappingInfo对象,其内部的属性读者有兴趣可自行去分析。

此处对createRequestMappingInfo()方法作下补充

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

小结

  • 介绍AbstractHandlerMethodMapping如何创建HandlerMethod,调用者为RequestMappingHandlerMapping,其可通过mvc:annotation-driven注册,具体可查看>>>SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

  • 其中其唯一实现类RequestMappingHandlerMapping主要是获取@Controller下的@RequestMapping注解的方法将其注册为HandlerMethod对象,其余的相关信息则注册为RequestMappingInfo对象供对路径信息匹配

  • 其中如何获取HandlerMethod查看前言中的链接即可

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898