国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringBoot擴展SpringMVC原理并實現全面接管

瀏覽:41日期:2023-04-11 17:41:10

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

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

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

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

WebMvcConfigurer方法介紹:這里只列舉幾個比較關鍵的方法

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

擴展MVC的實現原理:

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

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

點進這個注解,發現他還是WebMvcAutoConfiguration里的一個靜態內部類,但他繼承了DelegatingWebMvcConfiguration

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

再點進這個DelegatingWebMvcConfiguration類里,開頭有這樣一段代碼,有一個configurers屬性,類型是WebMvcConfigurerComposite ,這個WebMvcConfigurerComposite類也實現了WebMvcConfigurer,當@Autowired標注在一個方法上說明,這個方法的參數都從容器中獲取,這里是從容器中獲取所有的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); } }}

在這個類往下看,發現這個類的方法跟WebMvcConfigurer接口里的方法一樣,以這個視圖解析器舉例,方法里調用了這個方法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()都執行一遍。<mark style='box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;'>因為我們自己寫的配置類也注入到了容器里,所以我們的配置也會被調用,并且也被SpringBoot自動配置上,所以SpringMVC的自動配置和我們的擴展配置都會起作用</mark>;

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

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

因為一但標注了@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@EnableWebMvcpublic class myConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController('/index').setViewName('index'); }}

這個注解導入了@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)這個注解的意思就是容器中沒有這個組件的時候,這個自動配置類才生效

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久综合免费视频 | 日产一区二区三区四区 | 久久99国产精品亚洲 | 久久99亚洲精品久久 | 国产在线精品香蕉综合网一区 | 国产2区| 日本一区二区三区高清在线观看 | 日本一级毛片在线看 | 国产精品三级在线观看 | 国产乱码精品一区二区三区卡 | 日韩一区国产二区欧美三区 | 我要看欧美精品一级毛片 | 久久不色 | 国产精品福利午夜一级毛片 | 亚洲羞羞裸色私人影院 | 久久精品青草社区 | 久久久久久日本一区99 | 久久精品国产福利 | 在线播放一区二区精品产 | 国产高清免费影视在线观看 | 国产精选一区 | 欧美成人影院 | 国产精品国产三级国产专 | 欧美一级毛片欧美一级无片 | 92精品国产自产在线 | 丝袜毛片 | 亚洲国产剧情在线精品视 | 免费刺激视频 | rion美乳弹出来四虎在线观看 | 久久久久久久一线毛片 | 久久最新免费视频 | 又黄又爽又刺激的视频 | 欧美人成在线观看网站高清 | 特级深夜a级毛片免费观看 特级生活片 | 国产成人永久免费视频 | 中国一级毛片特级毛片 | 亚洲欧洲日产国码二区在线 | 免费a级毛片大学生免费观看 | 国产在线观看精品一区二区三区91 | 日韩欧美国产一区二区三区 | www欧美com|