说在前面
本次主要介绍springmvc配置解析。关注“天河聊架构”更多精彩。
springmvc配置解析
@EnableWebMvc这个注解干了什么,初始化defaultServletHandlerMapping
进入到这个方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#defaultServletHandlerMapping
@Bean public HandlerMapping defaultServletHandlerMapping() { // 创建DefaultServletHandlerConfigurer DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext); // 配置DefaultServletHandlerConfigurer -》 configureDefaultServletHandling(configurer); // 构建HandlerMapping -》 HandlerMapping handlerMapping = configurer.buildHandlerMapping(); return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping()); }
进入到这个方法org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#configureDefaultServletHandling
@Override protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { this.configurers.configureDefaultServletHandling(configurer); }
往上返回到这个方法org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer#buildHandlerMapping
protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.handler == null) { return null; } // 创建SimpleUrlHandlerMapping SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler)); handlerMapping.setOrder(Integer.MAX_VALUE); return handlerMapping; }
往上返回到这个方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#defaultServletHandlerMapping
说到最后
本次源码解析仅代表个人观点,仅供参考。