SpringMVC何时加载的controller里的mapping方法

简介: 疑问其实我一直有一个疑问,因为我在跟自己写的controller的生命周期源码的时候,没有发现解析mapping的代码,然后我就在想,什么时候解析并加载的mapping呢???结果是一个新的类 RequestMappingHandlerMapping ,惊呆了,我的小伙伴

demo下载


Demooo/springmvc-demo-app at master · cbeann/Demooo · GitHub


源码跟进


我就从RequestMappingHandlerMapping的生命周期的afterPropertiesSet()方法讲起


//RequestMappingHandlerMapping
@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());
        //调用父类的方法,跟进去--->
    super.afterPropertiesSet();
  }


//AbstractHandlerMethodMapping
protected void initHandlerMethods() {
        //遍历所有的类
    for (String beanName : getCandidateBeanNames()) {
      if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
                //当beanName为自定义的helloController时跟进去--->
        processCandidateBean(beanName);
      }
    }
    handlerMethodsInitialized(getHandlerMethods());
  }


//AbstractHandlerMethodMapping
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);
      }
    }
        //如果此类上有Controller或者RequestMapping,则返回真
    if (beanType != null && isHandler(beanType)) {
            //跟进去--->
      detectHandlerMethods(beanName);
    }


AbstractHandlerMethodMapping
protected void detectHandlerMethods(Object handler) {
    Class<?> handlerType = (handler instanceof String ?
        obtainApplicationContext().getType((String) handler) : handler.getClass());
    if (handlerType != null) {
      Class<?> userType = ClassUtils.getUserClass(handlerType);
            //解析类并且获取所有带有requestMapping的方法
      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和mapping都注入到某个地方(map)中,后面就可以直接使用了
      methods.forEach((method, mapping) -> {
        Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
        registerHandlerMethod(handler, invocableMethod, mapping);
      });
    }
  }


此时就已经明白了,并不是在自定义的controller类的生命周期内解析的mapping,而是在RequestMappingHandlerMapping  生命周期的afterPropertiesSet()方法中获取所有的controller类并解析

目录
相关文章
|
5月前
|
前端开发 Java 容器
SpringBoot中注册Servlet、Filter和Listener(代码和注解两种方式)
SpringBoot中注册Servlet、Filter和Listener(代码和注解两种方式)
59 0
|
5月前
Servlet3.0+环境下使用注解注册Servlet、Filter和Listener组件
Servlet3.0+环境下使用注解注册Servlet、Filter和Listener组件
42 2
|
5月前
|
XML 前端开发 JavaScript
SpringMVC中单独配置<mvc:default-servlet-handler/> 导致 Controller失效
SpringMVC中单独配置<mvc:default-servlet-handler/> 导致 Controller失效
84 0
|
10月前
|
Java
【SpringBoot】WebMvcConfigurer实现类不被加载(o.s.web.servlet.PageNotFound : No mapping for GET)的问题解决
【SpringBoot】WebMvcConfigurer实现类不被加载(o.s.web.servlet.PageNotFound : No mapping for GET)的问题解决
305 0
|
XML JSON 前端开发
SpringMVC【开发Controller】详解(一)
本文主要是讲解在Controller中的开发
212 0
SpringMVC【开发Controller】详解(一)
|
SQL JSON Oracle
SpringMVC【开发Controller】详解(四)
本文主要是讲解在Controller中的开发
176 0
SpringMVC【开发Controller】详解(四)
|
Java Spring
SpringMVC【开发Controller】详解(三)
本文主要是讲解在Controller中的开发
165 0
SpringMVC【开发Controller】详解(三)
SpringMVC【开发Controller】详解(二)
本文主要是讲解在Controller中的开发
139 0
SpringMVC【开发Controller】详解(二)
|
前端开发 Java 数据格式
springMVC 不扫描 controller 中的方法
最近把之前的一个Maven项目在一个新的电脑环境上导入Eclipse,启动时却发现不扫描 controller 中的方法 下面是正确的 spring-mvc.xml 文件 text/html;charset=UTF-8 ...
1130 0
|
Java Apache 容器
Spring Boot Controller 返回jsp页面结果404
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/79708989 ...
1127 0