您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用SpringBoot如何實(shí)現(xiàn)全面接管擴(kuò)展SpringMVC,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
如果想在SpringBoot中擴(kuò)展一些SpringMVC的配置,例如需要配置自定義的視圖解析器或攔截器等,需要怎么實(shí)現(xiàn)呢?
例如,自定義一個(gè)視圖解析器:
@Configuration public class MyConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("index"); } }
我們只需要編寫一個(gè)配置類去實(shí)現(xiàn)WebMvcConfigurer接口,并選擇實(shí)現(xiàn)接口中的方法,不能標(biāo)注@EnableWebMvc,這些WebMvcConfigurer接口中的方法就是SpringMVC所可以擴(kuò)展的配置
注意:在SpringBoot1.0版本中擴(kuò)展SpringMVC配置是繼承WebMvcConfigurerAdapter類,但在2.0以上的版本中已經(jīng)過時(shí),官方推薦使用以上實(shí)現(xiàn)WebMvcConfigurer接口的方式進(jìn)行擴(kuò)展,因?yàn)樵?.0版本中WebMvcConfigurer接口有了默認(rèn)實(shí)現(xiàn)。
WebMvcConfigurer方法介紹:這里只列舉幾個(gè)比較關(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āng)在做其他配置導(dǎo)入時(shí),導(dǎo)入了@Import(EnableWebMvcConfiguration.class)這樣一個(gè)注解,這個(gè)注解有什么用?
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
}
點(diǎn)進(jìn)這個(gè)注解,發(fā)現(xiàn)他還是WebMvcAutoConfiguration里的一個(gè)靜態(tài)內(nèi)部類,但他繼承了DelegatingWebMvcConfiguration
@Configuration(proxyBeanMethods = false)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
}
再點(diǎn)進(jìn)這個(gè)DelegatingWebMvcConfiguration類里,開頭有這樣一段代碼,有一個(gè)configurers屬性,類型是WebMvcConfigurerComposite ,這個(gè)WebMvcConfigurerComposite類也實(shí)現(xiàn)了WebMvcConfigurer,當(dāng)@Autowired標(biāo)注在一個(gè)方法上說明,這個(gè)方法的參數(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); } } }
在這個(gè)類往下看,發(fā)現(xiàn)這個(gè)類的方法跟WebMvcConfigurer接口里的方法一樣,以這個(gè)視圖解析器舉例,方法里調(diào)用了這個(gè)方法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),這個(gè)方法是把容器中所有的addViewControllers()都執(zhí)行一遍。<mark >因?yàn)槲覀冏约簩懙呐渲妙愐沧⑷氲搅巳萜骼?,所以我們的配置也?huì)被調(diào)用,并且也被SpringBoot自動(dòng)配置上,所以SpringMVC的自動(dòng)配置和我們的擴(kuò)展配置都會(huì)起作用</mark>;
class WebMvcConfigurerComposite implements WebMvcConfigurer { ... @Override public void addViewControllers(ViewControllerRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { delegate.addViewControllers(registry); } } }
還有上面在寫自定義配置類時(shí)為什么不能標(biāo)注@EnableWebMvc
因?yàn)橐坏珮?biāo)注了@EnableWebMvc,所有都是我們自己配置;所有的SpringMVC的自動(dòng)配置都失效了。<mark >原理又是怎么樣的?</mark>
給自己的配置類加上@EnableWebMvc
@Configuration @EnableWebMvc public class myConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("index"); } }
這個(gè)注解導(dǎo)入了@Import(DelegatingWebMvcConfiguration.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
這個(gè)類繼承了WebMvcConfigurationSupport
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}
我們再回頭看一下WebMvcAutoConfiguration,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)這個(gè)注解的意思就是容器中沒有這個(gè)組件的時(shí)候,這個(gè)自動(dòng)配置類才生效
以上就是使用SpringBoot如何實(shí)現(xiàn)全面接管擴(kuò)展SpringMVC,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。