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

SpringBoot擴展SpringMVC原理并實現(xiàn)全面接管

 更新時間:2020年11月05日 16:42:02   作者:程序員吉爾  
這篇文章主要介紹了SpringBoot擴展SpringMVC原理并實現(xiàn)全面接管,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

如果想在SpringBoot中擴展一些SpringMVC的配置,例如需要配置自定義的視圖解析器或攔截器等,需要怎么實現(xiàn)呢?
例如,自定義一個視圖解析器:

@Configuration
public class MyConfig implements WebMvcConfigurer {
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
 registry.addViewController("/index").setViewName("index");
 }
}

我們只需要編寫一個配置類去實現(xiàn)WebMvcConfigurer接口,并選擇實現(xiàn)接口中的方法,不能標(biāo)注@EnableWebMvc,這些WebMvcConfigurer接口中的方法就是SpringMVC所可以擴展的配置

注意:在SpringBoot1.0版本中擴展SpringMVC配置是繼承WebMvcConfigurerAdapter類,但在2.0以上的版本中已經(jīng)過時,官方推薦使用以上實現(xiàn)WebMvcConfigurer接口的方式進行擴展,因為在2.0版本中WebMvcConfigurer接口有了默認(rèn)實現(xiàn)。

WebMvcConfigurer方法介紹:這里只列舉幾個比較關(guān)鍵的方法

public interface WebMvcConfigurer {
 //定制URL匹配規(guī)則
 default void configurePathMatch(PathMatchConfigurer configurer) {
 }
 //內(nèi)容協(xié)商機制
 default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
 }
 //異步任務(wù)執(zhí)行器。
 default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
 }
 //使用默認(rèn)servlet處理靜態(tài)資源
 default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
 }
 //添加格式轉(zhuǎn)換器
 default void addFormatters(FormatterRegistry registry) {
 }
 //添加攔截器
 default void addInterceptors(InterceptorRegistry registry) {
 }
 //添加視圖解析器
 default void addViewControllers(ViewControllerRegistry registry) {
 }
}

擴展MVC的實現(xiàn)原理:

我們都知道WebMvcAutoConfiguration是SpringMVC的自動配置類,當(dāng)在做其他配置導(dǎo)入時,導(dǎo)入了@Import(EnableWebMvcConfiguration.class)這樣一個注解,這個注解有什么用?

@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
}

點進這個注解,發(fā)現(xiàn)他還是WebMvcAutoConfiguration里的一個靜態(tài)內(nèi)部類,但他繼承了DelegatingWebMvcConfiguration

@Configuration(proxyBeanMethods = false)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
}

再點進這個DelegatingWebMvcConfiguration類里,開頭有這樣一段代碼,有一個configurers屬性,類型是WebMvcConfigurerComposite ,這個WebMvcConfigurerComposite類也實現(xiàn)了WebMvcConfigurer,當(dāng)@Autowired標(biāo)注在一個方法上說明,這個方法的參數(shù)都從容器中獲取,這里是從容器中獲取所有的WebMvcConfigurer,并賦值給了configurers屬性

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

 @Autowired(required = false)
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
 if (!CollectionUtils.isEmpty(configurers)) {
  this.configurers.addWebMvcConfigurers(configurers);
 }
 }
}

在這個類往下看,發(fā)現(xiàn)這個類的方法跟WebMvcConfigurer接口里的方法一樣,以這個視圖解析器舉例,方法里調(diào)用了這個方法this.configurers.addViewControllers(registry)

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

 @Autowired(required = false)
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
 if (!CollectionUtils.isEmpty(configurers)) {
  this.configurers.addWebMvcConfigurers(configurers);
 }
 }

 ...

 @Override
 protected void addViewControllers(ViewControllerRegistry registry) {
 this.configurers.addViewControllers(registry);
 }
}

點進configurers.addViewControllers(registry),這個方法是把容器中所有的addViewControllers()都執(zhí)行一遍。<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">因為我們自己寫的配置類也注入到了容器里,所以我們的配置也會被調(diào)用,并且也被SpringBoot自動配置上,所以SpringMVC的自動配置和我們的擴展配置都會起作用</mark>;

class WebMvcConfigurerComposite implements WebMvcConfigurer {
 ...

 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
  for (WebMvcConfigurer delegate : this.delegates) {
  delegate.addViewControllers(registry);
  }
 }
}

還有上面在寫自定義配置類時為什么不能標(biāo)注@EnableWebMvc

因為一但標(biāo)注了@EnableWebMvc,所有都是我們自己配置;所有的SpringMVC的自動配置都失效了。<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">原理又是怎么樣的?</mark>

給自己的配置類加上@EnableWebMvc

@Configuration
@EnableWebMvc
public class myConfig implements WebMvcConfigurer {
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
 registry.addViewController("/index").setViewName("index");
 }
}

這個注解導(dǎo)入了@Import(DelegatingWebMvcConfiguration.class)

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

這個類繼承了WebMvcConfigurationSupport

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}

我們再回頭看一下WebMvcAutoConfiguration,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)這個注解的意思就是容器中沒有這個組件的時候,這個自動配置類才生效

小結(jié):大概了解到SpringBoot擴展SpringMVC的原理和全面接管SpringMVC,但SpringBoot中還有其他很多配置,只要了解其中的原理,其他配置也就一通百通了。

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

相關(guān)文章

  • Java集合ConcurrentHashMap詳解

    Java集合ConcurrentHashMap詳解

    ConcurrentHashMap?是?J.U.C?包里面提供的一個線程安全并且高效的?HashMap,所以ConcurrentHashMap?在并發(fā)編程的場景中使用的頻率比較高
    2023-01-01
  • java連接zookeeper的實現(xiàn)示例

    java連接zookeeper的實現(xiàn)示例

    ZooKeeper官方提供了Java API,可以通過Java代碼來連接zookeeper服務(wù)進行操作,本文就來介紹一下java連接zookeeper的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • Java面試題之MD5加密的安全性詳解

    Java面試題之MD5加密的安全性詳解

    MD5 是 Message Digest Algorithm 的縮寫,譯為信息摘要算法,它是 Java 語言中使用很廣泛的一種加密算法。本文將通過示例討論下MD5的安全性,感興趣的可以了解一下
    2022-10-10
  • SpringBoot實現(xiàn)在一個模塊中引入另一個模塊

    SpringBoot實現(xiàn)在一個模塊中引入另一個模塊

    這篇文章主要介紹了SpringBoot實現(xiàn)在一個模塊中引入另一個模塊的方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Springboot 整合 Java DL4J 實現(xiàn)時尚穿搭推薦系統(tǒng)(實例代碼)

    Springboot 整合 Java DL4J 實現(xiàn)時尚穿搭推薦系統(tǒng)(實例代碼)

    本文介紹了如何使用SpringBoot和JavaDeeplearning4j框架搭建一個時尚穿搭推薦系統(tǒng),文章詳細(xì)闡述了系統(tǒng)的技術(shù)架構(gòu)、數(shù)據(jù)集格式、Maven依賴配置、模型訓(xùn)練和預(yù)測代碼實現(xiàn),以及單元測試和預(yù)期輸出結(jié)果
    2024-10-10
  • Java繼承構(gòu)造器使用過程解析

    Java繼承構(gòu)造器使用過程解析

    這篇文章主要介紹了Java繼承構(gòu)造器使用過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Java獲取環(huán)境變量(System.getenv)的方法

    Java獲取環(huán)境變量(System.getenv)的方法

    本文主要介紹了Java獲取環(huán)境變量(System.getenv)的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Springboot中使用lombok的@Data注解方式

    Springboot中使用lombok的@Data注解方式

    這篇文章主要介紹了Springboot中使用lombok的@Data注解方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 基于Column注解的columnDefinition用法

    基于Column注解的columnDefinition用法

    這篇文章主要介紹了Column注解的columnDefinition用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot整合mybatis的方法詳解

    SpringBoot整合mybatis的方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合mybatis的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評論