日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot笔记整理(四)

發布時間:2024/7/19 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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、擴展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類型;不能標注@EnableWebMvc

既保留了所有的自動配置,也能用我們擴展的配置

//使用WebMvcConfigurer可以來擴展SpringMVC的功能 @Configuration public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {//瀏覽器發送/athomyit請求來到 successregistry.addViewController("/athomyit").setViewName("success");} }

原理:
1)WebMvcAutoConfiguration是SpringMVC的自動配置類。
2)在做其他自動配置時會導入:@Import(EnableWebMvcConfiguration.class)

@Configuration(proxyBeanMethods = false ) public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {//從容器中獲取所有的WebMVCConfigurer@Autowired(required = false ) public void setConfigurers(List<WebMvcConfigurer> configurers) {if (!CollectionUtils.isEmpty(configurers)) {this.configurers.addWebMvcConfigurers(configurers);}}

3)容器中所有的WebMvcConfigurer都會一起起作用

4)我們的配置類也會被調用
效果:SpringMVC的自動配置和我們的拓展配置都會起作用

3、全面接管SpringMVC

SpringBoot對SpringMVC的自動配置不需要了,所有的都是我們自己配置,所有的SpringMVC的自動配置都失效了

我們需要在配置類中添加@EnableWebMvc即可

//使用WebMvcConfigurerAdapter可以來擴展SpringMVC的功能 @EnableWebMvc @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//瀏覽器發送 /atguigu 請求來到 successregistry.addViewController("/atguigu").setViewName("success");} }

總結

以上是生活随笔為你收集整理的SpringBoot笔记整理(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。