java condition详解_《Java架构筑基》——详解SpringMvc中RequestCondition的作用

layout

title

no-post-nav

category

tags

description

post

SpringMvc中RequestCondition的作用

true

arch

arch

sSpringMvc中RequestCondition的作用

初始化

springmvc初始化时,会回调RequestMappingHandlerMapping中的afterPropertiesSet方法。

@Override

public void afterPropertiesSet() {

initHandlerMethods();

}

复制代码

追踪到initHandlerMethods中

if (beanType != null && isHandler(beanType)) {

detectHandlerMethods(beanName);

}

复制代码

isHander(beanType)见名知意,是对beanType类型判断,具体实现代码为

protected boolean isHandler(Class> beanType) {

return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||

AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));

}

复制代码

最关键的是detectHandlerMethods(beanName) 负责对所有的controller类进行处理

Map methods = MethodIntrospector.selectMethods(userType,

(MethodIntrospector.MetadataLookup) method -> {

return getMappingForMethod(method, userType);

});

复制代码

method为方法名,userType为当前处理的类。遍历该类中的所有方法,对方法进行参数路径请求方式等就行处理。下面看下getMappingForMethod是如何对不同方法进行处理的。

protected RequestMappingInfo getMappingForMethod(Method method, Class> handlerType) {

RequestMappingInfo info = createRequestMappingInfo(method);

if (info != null) {

RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); //handlerType 是类 这里为了解决类上的@RequestMapping使用

if (typeInfo != null) {

info = typeInfo.combine(info);

}

}

return info;

}

复制代码

可以看出为每个方法都创建了一个RequestMappingInfo,而RequestMappingInfo正是RequestCondition集合,记录了请路径、参数、方式、头等。可以看下对象内容

private final MyPatternsRequestCondition patternsCondition;

private final MyRequestMethodsRequestCondition methodsCondition;

private final MyParamsRequestCondition paramsCondition;

private final MyHeadersRequestCondition headersCondition;

private final MyConsumesRequestCondition consumesCondition;

private final MyProducesRequestCondition producesCondition;

private final MyRequestConditionHolder customConditionHolder;

复制代码

获取后的RequestMappingInfo会注册到HandlerMethod中

methods.forEach((method, mapping) -> {

Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);

registerHandlerMethod(handler, invocableMethod, mapping);

});

复制代码

最终注册到mappingRegistry中

protected void registerHandlerMethod(Object handler, Method method, T mapping) {

this.mappingRegistry.register(mapping, handler, method);

System.out.println("ssss");

}

复制代码

客户端发起请求

当客户端发起请求后,tomcat的servlet会转发到DispatcherServlet的servce函数回调。经过doDispatch,getHandler,再到AbstractHandlerMapping的getHandler,getHandlerInternal,lookupHandlerMethod看下lookupHandlerMethod代码

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {

List matches = new ArrayList<>();

List directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);

}

复制代码

这时,看到了我们初始化时,加载的mappingRegistry,这时就能匹配成功,找到对应的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值