springbootspringMVC擴展配置實現(xiàn)解析

摘要:

專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)天橋免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

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

我們也可以寫個自己的配置類,繼承 WebMvcConfigurer 重寫需要的配置方法 。在spring boot 早期是繼承WebMvcConfigurerAdapter ,但是高版已標(biāo)上注解@Deprecated,注意:在配置類中不要標(biāo)注:@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}) // 導(dǎo)入了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 標(biāo)記 @Configuration,并導(dǎo)入另一個內(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);
  }
 }
 ...//省略
 /**
  * 當(dāng)調(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 。當(dāng)調(diào)用 WebMvcConfigurer中 xxx方法的,就會遍歷 delegates 中所有 WebMvcConfigurer 的方法xxx 。那我們的擴展配置MyMvcConfig 也就被調(diào)用了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章題目:springbootspringMVC擴展配置實現(xiàn)解析
本文路徑:http://bm7419.com/article36/jcessg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司軟件開發(fā)、定制網(wǎng)站、域名注冊、微信公眾號、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營