javascript
SpringBoot笔记整理(四)
SpringBoot筆記整理(一)
SpringBoot筆記整理(二)
SpringBoot筆記整理(三)
SpringBoot筆記整理(四)
1、SpringMVC自動(dòng)配置
以下是SpringBoot對(duì)SpringMVC
-
nclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
自動(dòng)配置了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>編寫(xiě)一個(gè)配置類(@Configuration),是WebMvcConfigurationAdapter類型;不能標(biāo)注@EnableWebMvc
既保留了所有的自動(dòng)配置,也能用我們擴(kuò)展的配置
//使用WebMvcConfigurer可以來(lái)擴(kuò)展SpringMVC的功能 @Configuration public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {//瀏覽器發(fā)送/athomyit請(qǐng)求來(lái)到 successregistry.addViewController("/athomyit").setViewName("success");} }原理:
1)WebMvcAutoConfiguration是SpringMVC的自動(dòng)配置類。
2)在做其他自動(dòng)配置時(shí)會(huì)導(dǎo)入:@Import(EnableWebMvcConfiguration.class)
3)容器中所有的WebMvcConfigurer都會(huì)一起起作用
4)我們的配置類也會(huì)被調(diào)用
效果:SpringMVC的自動(dòng)配置和我們的拓展配置都會(huì)起作用
3、全面接管SpringMVC
SpringBoot對(duì)SpringMVC的自動(dòng)配置不需要了,所有的都是我們自己配置,所有的SpringMVC的自動(dòng)配置都失效了
我們需要在配置類中添加@EnableWebMvc即可
//使用WebMvcConfigurerAdapter可以來(lái)擴(kuò)展SpringMVC的功能 @EnableWebMvc @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//瀏覽器發(fā)送 /atguigu 請(qǐng)求來(lái)到 successregistry.addViewController("/atguigu").setViewName("success");} }總結(jié)
以上是生活随笔為你收集整理的SpringBoot笔记整理(四)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【计算机网络复习 数据链路层】3.5.4
- 下一篇: 总结下载Spring Framework