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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringBoot笔记整理(四)

發(fā)布時(shí)間:2024/7/19 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot笔记整理(四) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)

@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都會(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。