03spring boot web springmvc自动配置原理

springboot中使用springmvc
https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-developing-web-applications.html

springboot 为springmvc做了哪些自动配置
29.1.1 Spring MVC Auto-configuration

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
   *自动配置了ViewResolver(视图解析器:根据方法的返回值得的视图对象View),视图对象决定如何渲染(转发还是重定向)
    *ContentNegotiatingViewResolver :组合所有的视图解析器
    *可以自定义视图解析器
Support for serving static resources, including support for WebJars (covered later in this document)).
* 静态资源路径,webjars路径
Automatic registration of Converter, GenericConverter, and Formatter beans.
  *Converter:转换器,将页面传入的参数与方法中对象转换(类型转换)
  *Formatter :格式化器,字符串日期转Date;
  
Support for HttpMessageConverters (covered later in this document).
 *HttpMessageConverters :springMVC用来转换http请求和响应:对象转json
Automatic registration of MessageCodesResolver (covered later in this document).
Static index.html support.//静态首页
Custom Favicon support (covered later in this document) 默认图标.
Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

Formatter 格式化器
在这里插入图片描述扩展SpringMVC
配置文件的写法
在这里插入图片描述
用户自己写View http://127.0.0.1:8080/test 结果跳转到success.html页面
在这里插入图片描述原理
在这里插入图片描述 在这里插入图片描述
全面接管SpringMVC:
通过@EnableWebMvc配置Springboot对SpringMVC的自动配置不起作用,所有都是用户自己配置,在配置类中的地方加入@EnableWebMvc
在这里插入图片描述
原理 为什么添加@EnableWebMvc后SpringMVC的自动配置失效
在这里插入图片描述在这里插入图片描述如何修改springboot的默认配置
模式
1.springboot在自动配置很多组件的时候,先看容器中有没有用户自己配制的(@Bean,@Component),如果有就用用户的配置,如果没有就用自动配置,还有如果有些组件可以有多个(比如:ViewResolver)将用户配置的和自动默认的组合起来;
2.在springboot中会有非常多的xxxConfigurer帮助我们进行扩展配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章