1.WebMvcAutoConfiguration
EnableWebMvcConfiguration自动装配类负责加载SpringMVC涉及的HandlerAdapter
、HandlerMapping
、ExceptionHandlerExceptionResolver
等。
SpringMVC利用 DispatchServlet
处理上游Tomcat的请求时,会被HandlerMapping、HandlerAdapter的相关子类分别先后执行,这样做的原因是:
- 通过RequestUril找到相应的handler是因为HandlerMapping提前建立了 RequestUril & Handler 之间的映射关系。
- 不同HandlerMapping表明解析Handler上的Uri方式是不同的。
- HandlerAdapter触发执行 Uri 在Handler中的目标方法。
WebMvcAutoConfigurationAdapter自动装配类负责加载SpringMVC涉及的静态资源等Web资源。例如LocaleResolver、视图解析器InternalResourceViewResolver、ContentNegotiatingViewResolver等。
@Configuration(proxyBeanMethods = false)
public class WebMvcAutoConfiguration {
private static final String[] SERVLET_LOCATIONS = {
"/" };
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({
WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
...
@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
...}
...
}
// 该注解保证父类WebMvcConfigurationSupport也参与SpringBoot启动过程中各个注解的解析过程
@Configuration(proxyBeanMethods = false)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
...
@Bean
@Override
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager m,
@Qualifier("mvcConversionService") FormattingConversionService c,
@Qualifier("mvcValidator") Validator v) {
RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter(m,c, v);
boolean ignoreDefaultModelOnRedirect = this.mvcProperties.isIgnoreDefaultModelOnRedirect();
adapter.setIgnoreDefaultModelOnRedirect(this.mvcProperties == null || ignoreDefaultModelOnRedirect);
return adapter;
}
...
@Bean
@Primary
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager manager,
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
// 调用父类WebMvcConfigurationSupport
return super.requestMappingHandlerMapping(manager, conversionService,resourceUrlProvider);
}
...
}
}
1.2.WebMvcConfigurationSupport
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping(...}
@Bean
public PathMatcher mvcPathMatcher() {
...}
@Bean
public BeanNameUrlHandlerMapping beanNameHandlerMapping(...}
@Bean
public HandlerExceptionResolver handlerExceptionResolver(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager) {
List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<>();
configureHandlerExceptionResolvers(exceptionResolvers);
if (exceptionResolvers.isEmpty()) {
addDefaultHandlerExceptionResolvers(exceptionResolvers, contentNegotiationManager);
}