SpringMVC------AbstractHandlerMethodMapping

类结构

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

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

	/**
	 * Scan beans in the ApplicationContext, detect and register handler methods.
	 * @see #getCandidateBeanNames()
	 * @see #processCandidateBean
	 * @see #handlerMethodsInitialized
	 */
	protected void initHandlerMethods() {
        //获取springmvc上下文的所有注册的bean
		for (String beanName : getCandidateBeanNames()) {
			if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
                //根据名字获取类,判断该类是不是要转换的类型。
				processCandidateBean(beanName);
			}
		}
		handlerMethodsInitialized(getHandlerMethods());
	}

 

	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 - let's ignore it.
			if (logger.isTraceEnabled()) {
				logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
			}
		}
        //isHandler()是抽象方法,主要供子类需要扫描什么类型的bean
		if (beanType != null && isHandler(beanType)) {
            //调用detectHandlerMethods开始做类型和路径映射转换,解析其中的HandlerMethod进行注册
			detectHandlerMethods(beanName);
		}
	}
	@Override
	protected boolean isHandler(Class<?> beanType) {
		return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
				AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
	}

detectHandlerMethods()解析

	protected void detectHandlerMethods(Object handler) {
		Class<?> handlerType = (handler instanceof String ?
				obtainApplicationContext().getType((String) handler) : handler.getClass());

		if (handlerType != null) {
            //因为有些是CGLIB代理生成的,获取真实的类
			Class<?> userType = ClassUtils.getUserClass(handlerType);
			Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
					(MethodIntrospector.MetadataLookup<T>) method -> {
						try {
                            //模板方法获取handlerMethod的mapping属性,可扩展
							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));
			}
             //对查找到的HandlerMethod进行注册,保存至内部类mappingRegistry对象中
			methods.forEach((method, mapping) -> {
                /作下判断,method是否从属于userType
				Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
				registerHandlerMethod(handler, invocableMethod, mapping);
			});
		}
	}

看一下子类RequestMappingHandlerMapping

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

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值