承接上文。
在上一篇文章中,我们介绍到了SpringBoot是如何整合Tomcat的。我们知道在SpringBoot项目中可以使用SpringMVC的注解,比如@Controller和@RequestMapping注解。既然我们没有配置SpringMVC那么mvc的功能又是怎么起作用呢?
自动配置(一) 自动配置DispacherServlet和DispacherServletRegistration
首先找到SpringMVC的自动配置类
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
DispatcherServletAutoConfiguration.class
首先解释一下该类上的注解:根据行号来解释
1.@AutoConfigurationOrder:自动加载时配置该类的顺序
2.@Configuration:表示这是一个配置类
3.@ConditionalOnWebApplication:自动配置该类需要满足当前上下文环境是SERVLET环境
4.@ConditionalOnClass:classpath路径下需要存在DispacherServlet类
5.@AutoConfigureAfter: 在ServletWebServerFactoryAutoConfiguration后配置,这个ServletWebServerFactoryAutoConfiguation就是我们前面文章说的Tomcat的自动配置类
DispatcherServletAutoConfiguration类中有两个内部类分别是:DispatcherServletConfiguration、DispatcherServletRegistrationConfiguration
DispacherServletConfiguration.class
@Configuration(proxyBeanMethods = false)
@Conditional(DefaultDispatcherServletCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties({
HttpProperties.class, WebMvcProperties.class })
protected static class DispatcherServletConfiguration {
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
return dispatcherServlet;
}
// 这个类的主要作用是把用户自己配置的MultipartResolver的名称给配置一下
// 因为用户自己配置的类名称可能不叫multipartResolver
// 简单来说就是为了统一MultipartResolver在IOC容器中的bean名称叫 multipartResolver
@Bean
@ConditionalOnBean(MultipartResolver.class)
@ConditionalOnMissingBean(name =