亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot springMVC擴(kuò)展配置實(shí)現(xiàn)解析

 更新時(shí)間:2019年08月09日 09:00:51   作者:JonRain0625  
這篇文章主要介紹了spring boot springMVC擴(kuò)展配置實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

摘要:

在spring boot中 MVC這部分也有默認(rèn)自動(dòng)配置,也就是說我們不用做任何配置,那么也是OK的,這個(gè)配置類就是 WebMvcAutoConfiguration,但是也時(shí)候我們想設(shè)置自己的springMvc配置怎么辦呢 。

我們也可以寫個(gè)自己的配置類,繼承 WebMvcConfigurer 重寫需要的配置方法 。在spring boot 早期是繼承WebMvcConfigurerAdapter ,但是高版已標(biāo)上注解@Deprecated,注意:在配置類中不要標(biāo)注:@EnableWebMvc,否則,spring boot的配置全部失效,只留自己擴(kuò)展配置。

示例:

這里已高版為主 繼承WebMvcConfigurer,WebMvcConfigurer 接口中的方法都是默認(rèn)的方法,可以覆蓋,也可以不實(shí)現(xiàn) ,加一個(gè)視圖解析配置 ,解析success請(qǐng)求路勁,返回success頁面。如下代碼:

@Configuration
public class MyMvcConfig Implements WebMvcConfigurer {

 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
  // super.addViewControllers(registry);
  //瀏覽器發(fā)送 /success請(qǐng)求來到 success
  registry.addViewController("/success").setViewName("success");
 }
}

代碼淺析:

1.首先我們來看看WebMvcAutoConfiguration這個(gè)配置類,這個(gè)配置了有首頁的默認(rèn)路勁,還有一些靜態(tài)資源路勁,而這些方法在它的一個(gè)內(nèi)部類中,如下代碼(刪除了部分代碼):

@Configuration
@ConditionalOnWebApplication(
 type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
 ....//省略部分代碼
 @Configuration
 @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) // 導(dǎo)入了EnableWebMvcConfiguration這個(gè)類 addResourceHandlers方法
 @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
 @Order(0)
 public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {
    
    ...//省略部分代碼
  public void addResourceHandlers(ResourceHandlerRegistry registry) {//實(shí)現(xiàn)WebMvcConfigurer 這個(gè)類的
   if(!this.resourceProperties.isAddMappings()) {
    logger.debug("Default resource handling disabled");
   } else {
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    if(!registry.hasMappingForPattern("/webjars/**")) {
     this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }

    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if(!registry.hasMappingForPattern(staticPathPattern)) {
     this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }

   }
  }
}

可以看到,內(nèi)部類 WebMvcAutoConfigurationAdapter 標(biāo)記 @Configuration,并導(dǎo)入另一個(gè)內(nèi)部類 @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}),我們看下這個(gè)類,如下代碼:

@Configuration
 public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
  private final WebMvcProperties mvcProperties;
  private final ListableBeanFactory beanFactory;
  private final WebMvcRegistrations mvcRegistrations;
     ...// 省略
}

重點(diǎn)在它的父類, DelegatingWebMvcConfiguration 代碼如下 (寫了幾個(gè)案列方法,其他代碼省略。)。

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
  ...//省略
 /**
  * 從容器中拿到所有 WebMvcConfigurer 的實(shí)現(xiàn)類。遍歷添加到 configurers 
  * [required description]
  * @type {[type]}
  */
 @Autowired( required = false ) // 自動(dòng)裝配
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
  if(!CollectionUtils.isEmpty(configurers)) {
   this.configurers.addWebMvcConfigurers(configurers);
  }
 }
 ...//省略
 /**
  * 當(dāng)調(diào)用addResourceHandlers 時(shí) ,調(diào)用的 成員configurers的 addResourceHandlers
  * [addResourceHandlers description]
  * @param {[type]} ResourceHandlerRegistry registry [description]
  */
 protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  this.configurers.addResourceHandlers(registry);
 }
 ...//省略
}

來看看 WebMvcConfigurerComposite 的 addResourceHandlers的方法做了什么 :

class WebMvcConfigurerComposite implements WebMvcConfigurer {
 private final List<WebMvcConfigurer> delegates = new ArrayList<WebMvcConfigurer>();
 @Override
 public void addViewControllers(ViewControllerRegistry registry) { // 遍歷 把 所有WebMvcConfigurer的 addViewControllers方法調(diào)用一遍
  for (WebMvcConfigurer delegate : this.delegates) {
   delegate.addViewControllers(registry);
  }
 }
}

看到這里我們知道,不管是spring boot中實(shí)現(xiàn)的 WebMvcConfigurer 類,還是我們自己實(shí)現(xiàn) WebMvcConfigurer ,只要我們把實(shí)現(xiàn)類注入到容器中,就會(huì)被 注入 WebMvcConfigurerComposite 這個(gè)類成員變量 delegates中。

而 WebMvcConfigurerComposite 有是實(shí)現(xiàn)了 WebMvcConfigurer 。當(dāng)調(diào)用 WebMvcConfigurer中 xxx方法的,就會(huì)遍歷 delegates 中所有 WebMvcConfigurer 的方法xxx 。那我們的擴(kuò)展配置MyMvcConfig 也就被調(diào)用了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot事件發(fā)布與監(jiān)聽超詳細(xì)講解

    SpringBoot事件發(fā)布與監(jiān)聽超詳細(xì)講解

    今天去官網(wǎng)查看spring boot資料時(shí),在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • springBoot整合shiro如何解決讀取不到@value值問題

    springBoot整合shiro如何解決讀取不到@value值問題

    這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
    2023-08-08
  • 詳解Java中的do...while循環(huán)語句的使用方法

    詳解Java中的do...while循環(huán)語句的使用方法

    這篇文章主要介紹了Java中的do...while循環(huán)語句的使用方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-10-10
  • 淺談Java線程Thread.join方法解析

    淺談Java線程Thread.join方法解析

    本篇文章主要介紹了淺談Java線程Thread.join方法解析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • java利用Ant腳本生成war包全過程

    java利用Ant腳本生成war包全過程

    這篇文章主要為大家詳細(xì)介紹了java利用Ant腳本生成war包全過程,感興趣的朋友可以參考一下
    2016-03-03
  • Java詳解AVL樹的應(yīng)用

    Java詳解AVL樹的應(yīng)用

    AVL樹是高度平衡的二叉樹,它的特點(diǎn)是AVL樹中任何節(jié)點(diǎn)的兩個(gè)子樹的高度最大差別為1,本文主要給大家介紹了Java如何實(shí)現(xiàn)AVL樹,需要的朋友可以參考下
    2022-07-07
  • mybatis-generator-gui根據(jù)需求改動(dòng)示例

    mybatis-generator-gui根據(jù)需求改動(dòng)示例

    這篇文章主要為大家介紹了mybatis-generator-gui根據(jù)需求改動(dòng)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java中LambdaQueryWrapper的常用方法詳解

    Java中LambdaQueryWrapper的常用方法詳解

    這篇文章主要給大家介紹了關(guān)于Java中LambdaQueryWrapper常用方法的相關(guān)資料,lambdaquerywrapper是一個(gè)Java庫,用于構(gòu)建類型安全的Lambda表達(dá)式查詢,需要的朋友可以參考下
    2023-11-11
  • MyBatis分頁的四種方式

    MyBatis分頁的四種方式

    分頁查詢是非常常見的需求,本文主要介紹了MyBatis分頁的四種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 詳解Java實(shí)現(xiàn)分治算法

    詳解Java實(shí)現(xiàn)分治算法

    分治算法(divide and conquer)是五大常用算法(分治算法、動(dòng)態(tài)規(guī)劃算法、貪心算法、回溯法、分治界限法)之一,很多人在平時(shí)學(xué)習(xí)中可能只是知道分治算法,但是可能并沒有系統(tǒng)的學(xué)習(xí)分治算法,本篇就帶你較為全面的去認(rèn)識(shí)和了解分治算法
    2021-06-06

最新評(píng)論