javascript
SpringBoot笔记整理(四)
SpringBoot筆記整理(一)
SpringBoot筆記整理(二)
SpringBoot筆記整理(三)
SpringBoot筆記整理(四)
1、SpringMVC自動配置
以下是SpringBoot對SpringMVC
-
nclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
自動配置了ViewResolver -
Support for serving static resources, including support for WebJars .
-
registration of Converter, GenericConverter, and Formatter beans.
-
Support for HttpMessageConverters .
-
Automatic registration of MessageCodesResolver .
-
Static index.html support.
-
Custom Favicon support .
-
Automatic use of a ConfigurableWebindingInitializer bean .
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 class of type WebMvcConfigurerAdapter but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping,RequestMappingHandlerAdapter, or ExceptionHandExceptionResolver, 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.
2、擴(kuò)展SpringMVC
<mvc:view-controller path="/hello" view-name="success"/> <mvc:interceptors><mvc:interceptor><mvc:mapping path="/hello"/><bean></bean></mvc:interceptor> </mvc:interceptors>編寫一個配置類(@Configuration),是WebMvcConfigurationAdapter類型;不能標(biāo)注@EnableWebMvc
既保留了所有的自動配置,也能用我們擴(kuò)展的配置
//使用WebMvcConfigurer可以來擴(kuò)展SpringMVC的功能 @Configuration public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {//瀏覽器發(fā)送/athomyit請求來到 successregistry.addViewController("/athomyit").setViewName("success");} }原理:
1)WebMvcAutoConfiguration是SpringMVC的自動配置類。
2)在做其他自動配置時會導(dǎo)入:@Import(EnableWebMvcConfiguration.class)
3)容器中所有的WebMvcConfigurer都會一起起作用
4)我們的配置類也會被調(diào)用
效果:SpringMVC的自動配置和我們的拓展配置都會起作用
3、全面接管SpringMVC
SpringBoot對SpringMVC的自動配置不需要了,所有的都是我們自己配置,所有的SpringMVC的自動配置都失效了
我們需要在配置類中添加@EnableWebMvc即可
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能 @EnableWebMvc @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//瀏覽器發(fā)送 /atguigu 請求來到 successregistry.addViewController("/atguigu").setViewName("success");} }總結(jié)
以上是生活随笔為你收集整理的SpringBoot笔记整理(四)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计算机网络复习 数据链路层】3.5.4
- 下一篇: 数据库基础知识——视图