Insight Spring RequestMappingHandlerMapping

疑问:request请求是怎样find对应的controller

两种方法: 1、通过DispatcherServlet debug request的处理过程;

2、Insight spring-webmvc HandlerMapping 初始化过程。

本文通过方法2 Insight 初始化过程,解答find handler/request机制:

/**
 * HandlerMapping 实现核心Class
 * 核心属性urlMap (request/HanlderMethod)
 */
public abstract class AbstractHandlerMethodMapping
   
   
    
     implements InitializingBean {
    
    /**
	 * Spring bean 初始化入口,Detects handler methods at initialization.
	 * 1.Scan beans in the ApplicationContext(scan 的范围是context所有的bean),
	 * 2.detect and register handler methods(detect实现参考 RequestMappingHandlerMapping.isHandler()).
	 */
	public void afterPropertiesSet() {
		initHandlerMethods();
	}
	
	/**
	 * Register a handler method and its unique mapping.
	 * 最终会构造mapping: handlerMethods = (mapping : HandlerMethod(handler, method)), urlMap = (url : mapping)
	 */
	protected void registerHandlerMethod(Object handler, Method method, T mapping) {
		HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
		// ...
		this.handlerMethods.put(mapping, newHandlerMethod);
		// ...
		Set
    
    
     
      patterns = getMappingPathPatterns(mapping);
		for (String pattern : patterns) {
			if (!getPathMatcher().isPattern(pattern)) {
				this.urlMap.add(pattern, mapping);
			}
		}
	}
	
}/**
 * @Controller, @RequestMapping 注解HandlerMapping实现
 * extends AbstractHandlerMethodMapping
 */
public class RequestMappingHandlerMapping ... {
    
    /**
     * Whether the given type is a handler with handler methods.
     * Expects a handler to have a type-level @{@link Controller} annotation.
     */
    @Override
    protected boolean isHandler(Class
     
      beanType) {
    	return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
    			(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
    }
	
	
}
    
    
   
   
如上代码,结论是:

mvc-context分发request请求,根据url find mappingInfo, 然后根据mappingInfo find 对应的handler.method()。











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值