publicabstractclassAbstractHandlerMethodMapping<T>extendsAbstractHandlerMappingimplementsInitializingBean{protectedvoidinitHandlerMethods(){// 获取ApplicationContext容器中所有的beanName,循环判断不是以scopedTarget.开头的beanName执行候选操作。for(StringbeanName:getCandidateBeanNames()){if(!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)){// TODO 执行操作。processCandidateBean(beanName);}}// 一个钩子方法,在查找并封装完完所有的处理方法各种映射关系之后,进行调用。handlerMethodsInitialized(getHandlerMethods());}}
publicabstractclassAbstractHandlerMethodMapping<T>extendsAbstractHandlerMappingimplementsInitializingBean{protectedvoiddetectHandlerMethods(Objecthandler){Class<?>handlerType=(handlerinstanceofString?obtainApplicationContext().getType((String)handler):handler.getClass());if(handlerType!=null){Class<?>userType=ClassUtils.getUserClass(handlerType);// 获取方法 - RequestMappingInfo 的映射Map<Method,T>methods=MethodIntrospector.selectMethods(userType,(MethodIntrospector.MetadataLookup<T>)method->{try{// 根据类和方法上RequestMapping中的value,结合成一个路径封装成RequestMappingInfo返回returngetMappingForMethod(method,userType);}catch(Throwableex){thrownewIllegalStateException("Invalid mapping on handler class ["+userType.getName()+"]: "+method,ex);}});if(logger.isTraceEnabled()){logger.trace(formatMappings(userType,methods));}// TODO 注册处理方法methods.forEach((method,mapping)->{// 获取到可调用的方法MethodinvocableMethod=AopUtils.selectInvocableMethod(method,userType);// TODO 注册 HandlerMethodregisterHandlerMethod(handler,invocableMethod,mapping);});}}}
publicabstractclassAbstractHandlerMethodMapping<T>extendsAbstractHandlerMappingimplementsInitializingBean{protectedvoidregisterHandlerMethod(Objecthandler,Methodmethod,Tmapping){// TODO 注册映射信息this.mappingRegistry.register(mapping,handler,method);}}
publicclassHandlerMethod{/** Logger that is available to subclasses. */protectedfinalLoglogger=LogFactory.getLog(getClass());/** bean实例 */privatefinalObjectbean;/** beanFactory 工厂 */@NullableprivatefinalBeanFactorybeanFactory;/** bean的类型 */privatefinalClass<?>beanType;/** 方法 */privatefinalMethodmethod;/** 桥接方法 */privatefinalMethodbridgedMethod;/** 方法参数封装 */privatefinalMethodParameter[]parameters;// 省略其他变量...publicHandlerMethod(StringbeanName,BeanFactorybeanFactory,Methodmethod){// 省略无关代码...// 这里的变量 bean,仅仅封装了一个 beanName,并没有设置具体的实例。这个实例会在第一次调用controller方法时,去设置。this.bean=beanName;this.beanFactory=beanFactory;Class<?>beanType=beanFactory.getType(beanName);if(beanType==null){thrownewIllegalStateException("Cannot resolve bean type for bean with name '"+beanName+"'");}// 这里仅仅设置了一个beanType,这时候,bean实例并没有封装进来。在真正调用方法执行时,这个bean实例是必须有的。// 它在别的地方进行了设置。this.beanType=ClassUtils.getUserClass(beanType);this.method=method;this.bridgedMethod=BridgeMethodResolver.findBridgedMethod(method);this.parameters=initMethodParameters();evaluateResponseStatus();}}
publicclassRequestMappingHandlerAdapterextendsAbstractHandlerMethodAdapterimplementsBeanFactoryAware,InitializingBean{publicvoidafterPropertiesSet(){// TODO 在第一次执行时,会初始化ControllerAdvice缓存。// Do this first, it may add ResponseBody advice beansinitControllerAdviceCache();if(this.argumentResolvers==null){// TODO 参数解析器,里面注册了很多的参数解析类。实际上,参数的解析是一个十分复杂的过程,它的可能情况太多了。List<HandlerMethodArgumentResolver>resolvers=getDefaultArgumentResolvers();// 将所有的参数解析器封装到 argumentResolvers 中。this.argumentResolvers=newHandlerMethodArgumentResolverComposite().addResolvers(resolvers);}if(this.initBinderArgumentResolvers==null){// initBinder 参数解析器List<HandlerMethodArgumentResolver>resolvers=getDefaultInitBinderArgumentResolvers();this.initBinderArgumentResolvers=newHandlerMethodArgumentResolverComposite().addResolvers(resolvers);}if(this.returnValueHandlers==null){// 方法返回值处理类注册List<HandlerMethodReturnValueHandler>handlers=getDefaultReturnValueHandlers();this.returnValueHandlers=newHandlerMethodReturnValueHandlerComposite().addHandlers(handlers);}}}
publicclassRequestMappingHandlerAdapterextendsAbstractHandlerMethodAdapterimplementsBeanFactoryAware,InitializingBean{privateList<HandlerMethodReturnValueHandler>getDefaultReturnValueHandlers(){List<HandlerMethodReturnValueHandler>handlers=newArrayList<>();// 注册了很多返回值处理类,用于处理返回值的一些信息。// Single-purpose return value typeshandlers.add(newModelAndViewMethodReturnValueHandler());handlers.add(newModelMethodProcessor());handlers.add(newViewMethodReturnValueHandler());handlers.add(newResponseBodyEmitterReturnValueHandler(getMessageConverters(),this.reactiveAdapterRegistry,this.taskExecutor,this.contentNegotiationManager));handlers.add(newStreamingResponseBodyReturnValueHandler());handlers.add(newHttpEntityMethodProcessor(getMessageConverters(),this.contentNegotiationManager,this.requestResponseBodyAdvice));handlers.add(newHttpHeadersReturnValueHandler());handlers.add(newCallableMethodReturnValueHandler());handlers.add(newDeferredResultMethodReturnValueHandler());handlers.add(newAsyncTaskMethodReturnValueHandler(this.beanFactory));// Annotation-based return value typeshandlers.add(newModelAttributeMethodProcessor(false));handlers.add(newRequestResponseBodyMethodProcessor(getMessageConverters(),this.contentNegotiationManager,this.requestResponseBodyAdvice));// Multi-purpose return value typeshandlers.add(newViewNameMethodReturnValueHandler());handlers.add(newMapMethodProcessor());// Custom return value typesif(getCustomReturnValueHandlers()!=null){handlers.addAll(getCustomReturnValueHandlers());}// Catch-allif(!CollectionUtils.isEmpty(getModelAndViewResolvers())){handlers.add(newModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));}else{handlers.add(newModelAttributeMethodProcessor(true));}returnhandlers;}}