参考:官方文档 主要关注的类:WebMvcAutoConfiguration
以下是SpringBoot对SpringMVC的默认配置:
包括 ContentNegotiatingViewResolver 和BeanNameViewResolver 等组件.
BeanNameViewResolver: 使用视图的名字来解析视图ContentNegotiatingViewResolver:组合所有的视图解析器支持静态资源文件夹路径,webjars
支持静态首页配置
支持自定义 favicon.ico 图标
自动注册了 Converter,GenericConverter, Formatter 等组件
Converter:转换器—> 支持从一个原类型转换为一个目标类型;GenericConverter 支持在多个不同的原类型和目标类型之间进行转换Formatter 格式化器; @Bean @ConditionalOnProperty(prefix = "spring.mvc", name = "date-format") // 在文件中配置日期格式化的规则 public Formatter<Date> dateFormatter() { return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件 } 自己添加的格式化器转换器,我们只需要放在容器中即可
支持 HttpMessageConverters 消息转换
HttpMessageConverter:SpringMVC用来转换Http请求和响应的;( 例如,可以将对象自动转换为JSON 格式
HttpMessageConverters: 是从容器中确定;获取所有的HttpMessageConverter;
自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
MessageCodesResolver :定义错误代码生成规则
ConfigurableWebBindingInitializer: Spring MVC使用 WebBindingInitializer来初始化WebDataBinder特定请求。如果创建自己的ConfigurableWebBindingInitializer @Bean,Spring Boot会自动将Spring MVC配置为使用它。
org.springframework.boot.autoconfigure.web:web的所有自动场景;
总结:
如果要扩展Spring Boot MVC功能,如(拦截器,格式化程序,视图控制器等),可以添加自己的类实现WebMvcConfigurer接口,并在类上使用**@Configuration注解,但不要@EnableWebMvc**。
如果希望提供“RequestMappingHandlerMapping”,“ RequestMappingHandlerAdapter”或“ ExceptionHandlerExceptionResolver”的自定义实例,则可以声明一个提供此类组件的“ WebMvcRegistrationsAdapter”实例。
如果想完全接管SpringMVC,可以加上**@EnableWebMvc**(基于第一点
实现WebMvcConfigurer接口,重写里面的方法
注:SpringBoot已经默认有个静态内部类实现了该接口,WebMvcConfigurerAdapter。该类位于WebMvcAutoConfiguration类中
1.xx版本推荐继承WebMvcConfigurerAdapter来扩展springMVC,但2.xx版本,推荐直接实现WebMvcConfigurer接口来扩展
@Configuration public class MyMvcConfig implements WebMvcConfigurer { //添加视图控制器 @Override public void addViewControllers(ViewControllerRegistry registry) { // 访问/hello 跳转到success.html registry.addViewController("/hello").setViewName("success"); } // 所有的WebMvcConfigurer组件会一起起作用,前提是(将组件注册到容器 @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer(){ @Override public void addViewControllers(ViewControllerRegistry registry) { // 这里设置的viewName会到模板引擎文件夹(template)下寻找文件 registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); } // 添加拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { // 排除访问登录页面,以及登录请求 // 静态资源,SpringBoot 已经做好了映射,不用配置 registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**"). excludePathPatterns("/","/index.html","/user/login","/asserts/**","/webjars/bootstrap/**"); } }; } // 将国际化组件放到容器中 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } // 自定义内置tomcat容器配置 }SpringBoot对SpringMVC的自动配置全部失效,由自己配置(不推荐
在配置类中添加@EnableWebMvc
//添加@EnableWebMvc,全面接管mvc @EnableWebMvc @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } }1)@EnableWebMvc的核心
导入了DelegatingWebMvcConfiguration类
@Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc {2)、而DelegatingWebMvcConfiguration类继承自WebMvcConfigurationSupport
@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {3)、WebMvcAutoConfiguration自动配置类要求缺失WebMvcConfigurationSupport才生效
@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) //容器中没有这个组件的时候,这个自动配置类才生效 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) //... public class WebMvcAutoConfiguration {4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来,WebMvcAutoConfiguration自动失效
5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;