最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

摘要:

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

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

示例:

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

@Configuration
public class MyMvcConfig Implements WebMvcConfigurer {

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

代碼淺析:

1.首先我們來看看WebMvcAutoConfiguration這個配置類,這個配置了有首頁的默認路勁,還有一些靜態(tài)資源路勁,而這些方法在它的一個內(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}) // 導入了EnableWebMvcConfiguration這個類 addResourceHandlers方法
 @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
 @Order(0)
 public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {
    
    ...//省略部分代碼
  public void addResourceHandlers(ResourceHandlerRegistry registry) {//實現(xiàn)WebMvcConfigurer 這個類的
   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 標記 @Configuration,并導入另一個內(nèi)部類 @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}),我們看下這個類,如下代碼:

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

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

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
  ...//省略
 /**
  * 從容器中拿到所有 WebMvcConfigurer 的實現(xiàn)類。遍歷添加到 configurers 
  * [required description]
  * @type {[type]}
  */
 @Autowired( required = false ) // 自動裝配
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
  if(!CollectionUtils.isEmpty(configurers)) {
   this.configurers.addWebMvcConfigurers(configurers);
  }
 }
 ...//省略
 /**
  * 當調(diào)用addResourceHandlers 時 ,調(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中實現(xiàn)的 WebMvcConfigurer 類,還是我們自己實現(xiàn) WebMvcConfigurer ,只要我們把實現(xiàn)類注入到容器中,就會被 注入 WebMvcConfigurerComposite 這個類成員變量 delegates中。

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring Cloud 系列之負載均衡 Ribbon的示例代碼

    Spring Cloud 系列之負載均衡 Ribbon的示例代碼

    Ribbon 是 Netflix 發(fā)布的負載均衡器,它有助于控制 HTTP 和 TCP 客戶端的行為。這篇文章主要介紹了Spring Cloud 系列之負載均衡 Ribbon的示例代碼,需要的朋友可以參考下
    2020-11-11
  • java 中 MD5加密的實例

    java 中 MD5加密的實例

    這篇文章主要介紹了java 中 MD5加密的實例的相關資料,通過本文希望能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Java實現(xiàn)導入csv的示例代碼

    Java實現(xiàn)導入csv的示例代碼

    這篇文章主要為大家詳細介紹了Java實現(xiàn)導入csv的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • java編程之基于SpringBoot框架實現(xiàn)掃碼登錄

    java編程之基于SpringBoot框架實現(xiàn)掃碼登錄

    本文將介紹基于SpringBoot + Vue + Android實現(xiàn)的掃碼登錄demo的總體思路,文中附含詳細示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • java IO流將一個文件拆分為多個子文件代碼示例

    java IO流將一個文件拆分為多個子文件代碼示例

    這篇文章主要介紹了java IO流將一個文件拆分為多個子文件代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • SpringBoot?自定義starter?yaml提示失效問題及解決方法

    SpringBoot?自定義starter?yaml提示失效問題及解決方法

    在自定義starter后,必不可少會有properties配置參數(shù)需要指定,而在有時又不知道為什么出現(xiàn)這個問題,這篇文章主要介紹了SpringBoot?自定義starter?yaml提示失效問題,需要的朋友可以參考下
    2022-12-12
  • java 中的static關鍵字和final關鍵字的不同之處

    java 中的static關鍵字和final關鍵字的不同之處

    java 中的static關鍵字和final關鍵字的不同之處,需要的朋友可以參考一下
    2013-03-03
  • WeakHashMap的垃圾回收原理詳解

    WeakHashMap的垃圾回收原理詳解

    這篇文章主要介紹了WeakHashMap的垃圾回收原理詳解,WeakHashMap 與 HashMap 的用法基本類似,與 HashMap 的區(qū)別在于,HashMap的key保留了對實際對象的強引用個,這意味著只要該HashMap對象不被銷毀,該HashMap的所有key所引用的對象就不會被垃圾回收,需要的朋友可以參考下
    2023-09-09
  • spring MVC中接口參數(shù)解析的過程詳解

    spring MVC中接口參數(shù)解析的過程詳解

    這篇文章主要給大家介紹了關于spring MVC中接口參數(shù)解析的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring mvc具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • java如何獲取本地操作系統(tǒng)進程列表

    java如何獲取本地操作系統(tǒng)進程列表

    本文介紹了java中是如何獲取當前本地操作系統(tǒng)正在運行的系統(tǒng)進程的信息,需要的朋友可以參考下
    2015-07-07

最新評論

商水县| 宝清县| 萨嘎县| 稻城县| 白山市| 商都县| 荔波县| 信阳市| 西峡县| 永康市| 上栗县| 长沙县| 乐清市| 托里县| 上林县| 疏勒县| 河北省| 呼图壁县| 太仓市| 文登市| 乐安县| 彭泽县| 宜兰市| 休宁县| 龙陵县| 新疆| 杭锦旗| 贵州省| 琼海市| 九江县| 鹤岗市| 祥云县| 科尔| 朝阳县| 靖远县| 富顺县| 稻城县| 龙陵县| 富川| 贵溪市| 临夏县|