spring requestMapping注解将url映射到controller方法

1url 映射的handler(即controller) 会放在RequestMappingHandlerMapping 类中, RequestMappingHandlerMapping的父类是AbstractHandlerMethodMapping AbstractHandlerMethodMapping 实现了InitializingBean

  1. RequestMappingHandlerMapping 覆盖了父类AbstractHandlerMethodMapping 的afterPropertiesSet 方法
@Override
	public void afterPropertiesSet() {
          
   
		this.config = new RequestMappingInfo.BuilderConfiguration();
		this.config.setUrlPathHelper(getUrlPathHelper());
		this.config.setPathMatcher(getPathMatcher());
		this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
		this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
		this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
		this.config.setContentNegotiationManager(getContentNegotiationManager());
         
         // 调用父类的afterPropertiesSet  方法
		super.afterPropertiesSet();
	}

AbstractHandlerMethodMapping 的afterPropertiesSet 方法

@Override
	public void afterPropertiesSet() {
          
   
		initHandlerMethods();
	}
  1. 初始化handler 方法
protected void initHandlerMethods() {
          
   
	    // 遍历所有bean
		for (String beanName : getCandidateBeanNames()) {
          
   
			if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
          
   
			    // 处理候选bean
				processCandidateBean(beanName);
			}
		}
		handlerMethodsInitialized(getHandlerMethods());
	}
  1. 处理候选bean
protected void processCandidateBean(String beanName) {
          
   
		Class<?> beanType = null;
		try {
          
   
			beanType = obtainApplicationContext().getType(beanName);
		}
		catch (Throwable ex) {
          
   
			// An unresolvable bean type, probably from a lazy bean - lets ignore it.
			if (logger.isTraceEnabled()) {
          
   
				logger.trace("Could not resolve type for bean " + beanName + "", ex);
			}
		}
		if (beanType != null && isHandler(beanType)) {
          
   
		    // 检测handler 方法(即controller的method)
			detectHandlerMethods(beanName);
		}
	}
  1. 检测handler 方法(即controller的method)
protected void detectHandlerMethods(Object handler) {
          
   
		Class<?> handlerType = (handler instanceof String ?
				obtainApplicationContext().getType((String) handler) : handler.getClass());

		if (handlerType != null) {
          
   
			Class<?> userType = ClassUtils.getUserClass(handlerType);
			Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
					(MethodIntrospector.MetadataLookup<T>) method -> {
          
   
						try {
          
   
							return getMappingForMethod(method, userType);
						}
						catch (Throwable ex) {
          
   
							throw new IllegalStateException("Invalid mapping on handler class [" +
									userType.getName() + "]: " + method, ex);
						}
					});
			if (logger.isTraceEnabled()) {
          
   
				logger.trace(formatMappings(userType, methods));
			}
			// method  对应controller的方法;   mapping 对应请求的url
			methods.forEach((method, mapping) -> {
          
   
				Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
				// 注册handler 方法
				registerHandlerMethod(handler, invocableMethod, mapping);
			});
		}
	}
  1. 注册handler 方法
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
          
   
		this.mappingRegistry.register(mapping, handler, method);
	}

看图就知道映射的url 和方法了

url 请求过来的时候就会走如下路径去获取 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#mappingRegistry 中的 handler方法

org.springframework.web.servlet.DispatcherServlet#doDispatch org.springframework.web.servlet.DispatcherServlet#getHandler org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal
经验分享 程序员 微信小程序 职场和发展