(一)DispatcherServlet加入到Servlet里面


在spi里面有
C:\Users\Administrator.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar!\META-INF\spring.factories

里面有个org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,

  1. @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
  2. @Configuration(proxyBeanMethods = false)
  3. @ConditionalOnWebApplication(type = Type.SERVLET)
  4. @ConditionalOnClass(DispatcherServlet.class)
  5. @AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
  6. public class DispatcherServletAutoConfiguration {
  7. /*
  8. * The bean name for a DispatcherServlet that will be mapped to the root URL "/"
  9. */
  10. public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";
  11. /*
  12. * The bean name for a ServletRegistrationBean for the DispatcherServlet "/"
  13. */
  14. public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";
  15. @Configuration(proxyBeanMethods = false)
  16. @Conditional(DefaultDispatcherServletCondition.class)
  17. @ConditionalOnClass(ServletRegistration.class)
  18. @EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class })//加载配置文件
  19. protected static class DispatcherServletConfiguration {
  20. @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
  21. public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
  22. //创建对象
  23. DispatcherServlet dispatcherServlet = new DispatcherServlet();
  24. dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
  25. dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
  26. dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
  27. dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
  28. dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
  29. return dispatcherServlet;//加入到Spring容器里面
  30. }
  31. @Bean
  32. @ConditionalOnBean(MultipartResolver.class)
  33. @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
  34. public MultipartResolver multipartResolver(MultipartResolver resolver) {
  35. // Detect if the user has created a MultipartResolver but named it incorrectly
  36. return resolver;
  37. }
  38. }
  39. @Configuration(proxyBeanMethods = false)
  40. @Conditional(DispatcherServletRegistrationCondition.class)
  41. @ConditionalOnClass(ServletRegistration.class)
  42. @EnableConfigurationProperties(WebMvcProperties.class)
  43. @Import(DispatcherServletConfiguration.class)
  44. protected static class DispatcherServletRegistrationConfiguration {
  45. /**
  46. * 加入到Servlet容器
  47. */
  48. @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
  49. @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
  50. public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
  51. WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
  52. DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
  53. webMvcProperties.getServlet().getPath());
  54. registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
  55. //配置加入方式,随着Tomcat启动而启动
  56. registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
  57. multipartConfig.ifAvailable(registration::setMultipartConfig);
  58. return registration;
  59. }
  60. }
  61. //...........
  62. }





(二)HandlerMapping和HandlerAdapter配置



在spi里面有
C:\Users\Administrator.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar!\META-INF\spring.factories
通过SPI方式加载
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\


org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter内部类上面@Import了EnableWebMvcConfiguration类

EnableWebMvcConfiguration extends 了DelegatingWebMvcConfiguration extends 了WebMvcConfigurationSupport

在WebMvcConfigurationSupport里面逻辑
1————————

org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#requestMappingHandlerAdapter
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#requestMappingHandlerMapping
两个加入到Spring容器里面

1————————