av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

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

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

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

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

public interface WebMvcConfigurer { //定制URL匹配規(guī)則 default void configurePathMatch(PathMatchConfigurer configurer) { } //內(nèi)容協(xié)商機(jī)制 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) { }}

擴(kuò)展MVC的實(shí)現(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 {}

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

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

再點(diǎn)進(jìn)這個DelegatingWebMvcConfiguration類里,開頭有這樣一段代碼,有一個configurers屬性,類型是WebMvcConfigurerComposite ,這個WebMvcConfigurerComposite類也實(shí)現(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); }}

點(diǎn)進(jìn)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;'>因?yàn)槲覀冏约簩懙呐渲妙愐沧⑷氲搅巳萜骼铮晕覀兊呐渲靡矔徽{(diào)用,并且也被SpringBoot自動配置上,所以SpringMVC的自動配置和我們的擴(kuò)展配置都會起作用</mark>;

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

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

因?yàn)橐坏珮?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@EnableWebMvcpublic 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擴(kuò)展SpringMVC的原理和全面接管SpringMVC,但SpringBoot中還有其他很多配置,只要了解其中的原理,其他配置也就一通百通了。

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲成人免费在线观看 | 美国式禁忌14在线 | 日韩一级在线观看 | 精品日韩在线 | 在线观看欧美日韩视频 | 久久一级视频 | 清清草视频 | 黄色大片在线播放 | 精品国产乱码久久久久久蜜柚 | 黄色录像免费看 | 亚洲天堂一区 | 黄色在线视频播放 | 成年免费视频黄网站在线观看 | 日韩一区二区免费视频 | 国产日批视频 | 亚洲淫片 | 欧美综合一区二区三区 | 黄色a一级 | 日狠狠| 成人午夜激情 | 亚洲精品在线视频观看 | 四虎1515| 国产成人在线视频 | 国产视频导航 | 中文字幕一级片 | 在线视频91 | 黄色国产精品 | 91av免费观看 | 久久久久久久国产精品 | 久久亚洲天堂 | 天天干夜夜骑 | 四虎激情| 成人一区二区三区 | 免费观看全黄做爰视频 | 久热中文字幕 | 在线观看三级 | 午夜精品久久久 | 天天综合影院 | 国产一区二区三区精品视频 | 91精品国自产在线观看 | 免费国产一区 |