在springboot中添加mvc功能的正確姿勢講解
先放出來幾個類(包含注解或接口)來觀摩一下
WebMvcConfigurer @EnableWebMvc WebMvcConfigurerAdapter(已過時,不再詳述,可以理解為繼承該類有和實現WebMvcConfigurer一樣的效果) WebMvcConfigurationSupport WebApplicationInitializer這里只聊springboot或者無web.xml環境的情況,無論如何得看一下這個祖宗,以下代碼來源于spring官網
public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletCxt) {// Load Spring web application configurationAnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();ac.register(AppConfig.class);ac.refresh();// Create and register the DispatcherServletDispatcherServlet servlet = new DispatcherServlet(ac);ServletRegistration.Dynamic registration = servletCxt.addServlet('app', servlet);registration.setLoadOnStartup(1);registration.addMapping('/app/*'); }}
這個是一切無配置文件spring和springmvc整合的基礎,用來替代原始的web.xml中的ContextLoadListener和DispatcherServlet,兩者效果等同;
現在我們先基于上述代碼的情況來說,請注意以下結論的前提是基于上述示例代碼,很重要,其實就像是很久之前我們從零開始搭建整合方案一樣,現在只是配置了整合的類,還沒有功能,如果我們想要再配置一個json的消息轉換器
那么我們就會有如下幾種方案 繼承WebMvcConfigurationSupport 實現WebMvcConfigurer 繼承WebMvcConfigurerAdapter(已過時不再詳述)繼承WebMvcConfigurationSupport和實現WebMvcConfigurer的區別如下 WebMvcConfigurationSupport直接繼承并使用@Configuration標識即可,而實現WebMvcConfigurer則需要標識為注解@Configuration以外還需要使用注解@EnableWebMvc標識才可,所以一個項目中可以有多個地方去實現這個接口,只要標識為配置類。然后在一處地方開啟@EnableWebMvc就可。這里提前說一下如果是springboot環境這里還大有說頭,這里還有個大坑,留在后面說 WebMvcConfigurationSupport更偏向底層,可以定制化的功能更多,而WebMvcConfigurer是一個接口,是針對WebMvcConfigurationSupport功能將一些常用的功能選擇性的暴露出來,實際上WebMvcConfigurer是依賴于WebMvcConfigurationSupport來實現功能添加的為什么說WebMvcConfigurer是依賴于WebMvcConfigurationSupport來實現功能添加的?我們來看一下配合該接口的注解@EnableWebMvc源碼
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Documented@Import(DelegatingWebMvcConfiguration.class)public @interface EnableWebMvc {}
來看一下這個注解導入的配置類DelegatingWebMvcConfiguration為何物?以下摘取該類部分源碼
@Configurationpublic 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); } }}
首先這個配置類其實就是我們上面最開始說的用繼承來擴展功能的WebMvcConfigurationSupport,那么為什么實現WebMvcConfigurer接口也行?注意看上圖中的代碼,提供了一個configurers屬性, 然后通過setConfigurers方法注入將ioc容器中的所有實現了WebMvcConfigurer接口的配置類都添加到configurers中;后續實現代碼不是本章討論范圍,我也沒有往下看,單看這里其實就已經明白了。
**上述是整合的基礎,那么當我們在springboot中要添加功能的時候要注意一些什么事情呢?**這個也是寫這篇文章的目的,因為最近在項目中有人在擴展功能的時候去繼承了WebMvcConfigurationSupport這個類,然后聯想到之前的項目也有人在springboot項目中使用了注解@EnableWebMvc,這兩種情況都不會導致項目啟動報錯,但卻在不該使用的時候使用了這些功能,導致了項目其實是不能正常使用的。現在來看一下為什么?
首先看一下springboot給我們提供的自動整合類,請參考類
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
我來截取部分代碼
@Configuration@ConditionalOnWebApplication(type = Type.SERVLET)@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })public class WebMvcAutoConfiguration { @Configuration @Import(EnableWebMvcConfiguration.class) @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware { } /** * Configuration equivalent to {@code @EnableWebMvc}. */ @Configuration public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration { }}
現在說一下結論,springboot為我們提供的整合功能,已經默認的幫我們添加了很多功能,如消息轉換器,靜態資源映射,視圖解析器,看下WebMvcAutoConfiguration的內部類WebMvcAutoConfigurationAdapter,其實就是實現了接口WebMvcConfigurer,然后再通過注解@Import(EnableWebMvcConfiguration.class)又將EnableWebMvcConfiguration這個配置類導入了進來,而我們點進去發現這個類的作用其實就是等同于之前我們說過的@EnableWebMvc。因此我們說的消息轉換器啊,靜態資源映射,視圖解析器等這些默認實現就在WebMvcAutoConfiguration的內部類WebMvcAutoConfigurationAdapter又通過@Bean注入進來的
也就是說springboot其實幫我們整合好之后又默認幫我們做了一切常用的實現,這樣我們開箱即用的不僅是整合好的框架,還有一些約定大于配置的功能,如靜態資源要放在static下,其實就是默認幫我們做了資源映射,詳細可以看下
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#addResourceHandlers
默認整合的功能基本滿足于我們日常的開發,而如果我們還需要添加功能要怎么辦呢?其實就是直接實現接口WebMvcConfigurer然后將當前類使用注解@Configuration標識為配置類即可。
那么為什么不能再繼續繼承接口WebMvcConfigurationSupport了呢?還是來看一下我們的自動配置類WebMvcAutoConfiguration吧,仔細看一下上面的配置類上的條件表達式中有這么一句非常非常重要的@ConditionalOnMissingBean(WebMvcConfigurationSupport.class),
上面我們所屬的所有整合功能的前提是當前ioc容器中沒有WebMvcConfigurationSupport這個bean,我們看到了springboot自己的實現是在當前自動配置類生效的時候才通過實現接口WebMvcConfigurer的,所以容器中在當前配置類未執行之前也是沒有這個WebMvcConfigurationSupport的,現在我們突然在項目中添加功能的時候去繼承了這個類,然后標識為配置類之后,立馬在容器中就出現了這個bean,然后springboot就會以為我們要全面接管整合springmvc,我們要拋棄它的默認實現,然后自己玩。然后就悲劇了。現在整個mvc中反而只有我們自己新加的這個擴展空間了。這在絕大多數情況下根本不是我們想要的。
還有一個問題,為什么加注解@EnableWebMvc也不行?其實通過上面的講解我們已經能夠看出來,這個注解其實就是導入了DelegatingWebMvcConfiguration這個配置類,而這個類就是繼承WebMvcConfigurationSupport的,這兩個效果是相同的,所以也不行。
總而言之一句話,在WebMvcAutoConfiguration這個配置類執行之前,無論是繼承WebMvcConfigurationSupport還是在某個配置類上添加了注解@EnableWebMvc,都會導致容器中會被注入類型為WebMvcConfigurationSupport的bean,而springboot在實現自動配置時將這種行為定義成了你要自己去實現``
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. IDEA EasyCode 一鍵幫你生成所需代碼2. Ajax引擎 ajax請求步驟詳細代碼3. Java構建JDBC應用程序的實例操作4. Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案5. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)6. javascript設計模式 ? 建造者模式原理與應用實例分析7. 一篇文章帶你了解JavaScript-對象8. Python使用oslo.vmware管理ESXI虛擬機的示例參考9. IntelliJ IDEA設置條件斷點的方法步驟10. Express 框架中使用 EJS 模板引擎并結合 silly-datetime 庫進行日期格式化的實現方法
