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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot—SpringMVC自动配置原理以及扩展和全面接管SpringMVC

發(fā)布時間:2025/3/21 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot—SpringMVC自动配置原理以及扩展和全面接管SpringMVC 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1.以下是SpringBoot對SpringMVC的默認配置
    • 2、擴展SpringMVC
    • 3、全面接管SpringMVC;
    • 5、如何修改SpringBoot的默認配置

Spring MVC auto-configuration

1.以下是SpringBoot對SpringMVC的默認配置

(WebMvcAutoConfiguration)

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 自動配置了ViewResolver(視圖解析器:根據(jù)方法的返回值得到視圖對象(View),視圖對象決定如何渲染(轉(zhuǎn)發(fā)?重定向?))
    • ContentNegotiatingViewResolver:組合所有的視圖解析器的;
    • 如何定制:我們可以自己給容器中添加一個視圖解析器;自動的將其組合進來;
  • Support for serving static resources, including support for WebJars (see below).靜態(tài)資源文件夾路徑,webjars

  • Static index.html support. 靜態(tài)首頁訪問

  • Custom Favicon support (see below). favicon.ico圖標的自動配置

  • 自動注冊了 of Converter, GenericConverter, Formatter beans.

    • Converter:轉(zhuǎn)換器; public String hello(User user):類型轉(zhuǎn)換使用Converter
    • Formatter 格式化器; 2017.12.17===Date;
@Bean@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的規(guī)則public Formatter<Date> dateFormatter() {return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化組件}

? 自己添加的格式化器轉(zhuǎn)換器,我們只需要放在容器中即可

  • Support for HttpMessageConverters (see below).

    • HttpMessageConverter:SpringMVC用來轉(zhuǎn)換Http請求和響應(yīng)的;User—Json;

    • HttpMessageConverters 是從容器中確定;獲取所有的HttpMessageConverter;

      自己給容器中添加HttpMessageConverter,只需要將自己的組件注冊容器中(@Bean,@Component)

      ?

  • Automatic registration of MessageCodesResolver (see below).定義錯誤代碼生成規(guī)則

  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

    我們可以配置一個ConfigurableWebBindingInitializer來替換默認的;(添加到容器)

    初始化WebDataBinder; 請求數(shù)據(jù)=====JavaBean;

org.springframework.boot.autoconfigure.web:web的所有自動場景;

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing 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),是WebMvcConfigurerAdapter類型;不能標注@EnableWebMvc;

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

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

原理:

? 1)、WebMvcAutoConfiguration是SpringMVC的自動配置類

? 2)、在做其他自動配置時會導(dǎo)入;@Import(EnableWebMvcConfiguration.class)

@Configurationpublic static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();//從容器中獲取所有的WebMvcConfigurer@Autowired(required = false)public void setConfigurers(List<WebMvcConfigurer> configurers) {if (!CollectionUtils.isEmpty(configurers)) {this.configurers.addWebMvcConfigurers(configurers);//一個參考實現(xiàn);將所有的WebMvcConfigurer相關(guān)配置都來一起調(diào)用; @Override// public void addViewControllers(ViewControllerRegistry registry) {// for (WebMvcConfigurer delegate : this.delegates) {// delegate.addViewControllers(registry);// }}}}

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

? 4)、我們的配置類也會被調(diào)用;

? 效果:SpringMVC的自動配置和我們的擴展配置都會起作用;

3、全面接管SpringMVC;

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

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

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

原理:

為什么@EnableWebMvc自動配置就失效了;

1)@EnableWebMvc的核心

@Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc {

2)、

@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class,WebMvcConfigurerAdapter.class }) //容器中沒有這個組件的時候,這個自動配置類才生效 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {

4)、@EnableWebMvc將WebMvcConfigurationSupport組件導(dǎo)入進來;

5)、導(dǎo)入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

5、如何修改SpringBoot的默認配置

模式:

? 1)、SpringBoot在自動配置很多組件的時候,先看容器中有沒有用戶自己配置的(@Bean、@Component)如果有就用用戶配置的,如果沒有,才自動配置;如果有些組件可以有多個(ViewResolver)將用戶配置的和自己默認的組合起來;

? 2)、在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴展配置

? 3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進行定制配置

學(xué)習內(nèi)容來自尚硅谷

總結(jié)

以上是生活随笔為你收集整理的Spring Boot—SpringMVC自动配置原理以及扩展和全面接管SpringMVC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 波多野结衣一区二区三区在线观看 | 中文字幕国产在线观看 | 有码视频在线观看 | 日韩aa视频 | 丁香花五月 | 激情片网站 | 日本一二三区视频在线 | 人妻天天爽夜夜爽一区二区三区 | 精品黑人一区二区三区在线观看 | 一级精品视频 | 成人午夜福利一区二区 | 日本亚洲网站 | 色婷婷综合久久久久中文 | 97狠狠| 久久发布国产伦子伦精品 | 日韩黄色一级视频 | 麻豆成人久久精品一区二区三区 | 一色道久久88加勒比一 | 国产一区二区片 | 成人精品区 | 99热2| 欧美老女人性生活 | 超碰98| 国产稀缺真实呦乱在线 | 国产无码精品合集 | 国产欧美在线一区 | 77777av | 亚洲 激情 | 亚洲福利视频网站 | 日本乱偷人妻中文字幕在线 | 国产免费av电影 | 久久精品99久久久久久 | 亚洲图片自拍偷拍区 | 中国zzji女人高潮免费 | 男女激情四射网站 | 免费看91的网站 | 青青青国产视频 | 午夜手机福利 | 黑人巨大精品人妻一区二区 | 久久久久久久偷拍 | 少妇福利视频 | 三级网站在线 | 久久久久久久久久久综合 | 久久国产人妻一区二区免色戒电影 | 前任攻略在线观看免费完整版 | 99蜜桃臀久久久欧美精品网站 | 激情网站免费 | 96日本xxxxxⅹxxx70 | 性一交一乱一区二区洋洋av | 色www国产亚洲阿娇 自拍一区在线 | 天天干天天透 | 91黄漫 | 日韩激情一区二区三区 | 国产亚洲精品久久久久婷婷瑜伽 | 91视频在线免费 | 欧美大片91 | 精品国产自在精品国产精小说 | 女性毛片| 成人午夜免费网站 | 亚洲国产一区二区在线 | 欧美巨乳在线观看 | 中文字幕女优 | 国产欧美日韩精品一区二区三区 | 国产特黄一级片 | 黄色美女大片 | 伊人久久大香线 | 亚洲自啪 | 亚洲中文字幕无码专区 | 亚洲蜜桃av一区二区 | 中文字幕――色哟哟 | 美女网站全黄 | 永久免费成人代码 | 伊是香蕉大人久久 | 超碰在线播放97 | 大桥未久视频在线观看 | 亚洲日日夜夜 | 久久天天东北熟女毛茸茸 | 超碰免费公开在线 | av网页在线 | 亚洲综合免费 | 国产精品无码久久av | αv在线 | 无码国产精品高潮久久99 | 91www| 2019中文字幕在线免费观看 | 天天操网站| 一级黄色片毛片 | 欧美日韩精品一区二区在线播放 | 碰碰97| 亚洲av永久中文无码精品综合 | 一区二区三区高清在线 | 伊人久久大香线蕉成人综合网 | 亚洲六月丁香色婷婷综合久久 | 黄色免费看视频 | 国产精品久久久久久久一区探花 | 69xxxx国产 | 天天爽夜夜爽夜夜爽精品 | 日韩一二三四五区 | 完全免费在线视频 |