說在前面web
本次主要介紹springmvc配置解析。更多精彩請關注「天河聊架構」微信公衆號。spring
springmvc配置解析微信
@EnableWebMvc這個註解幹了什麼,初始化ViewResolver架構
進入到這個方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#mvcViewResolvermvc
@Bean public ViewResolver mvcViewResolver() { // 建立視圖解析器 -》 ViewResolverRegistry registry = new ViewResolverRegistry( mvcContentNegotiationManager(), this.applicationContext); // 配置視圖解析器 configureViewResolvers(registry); if (registry.getViewResolvers().isEmpty()) { // 從beanFactory中查找ViewResolver類型的視圖解析器的名字 String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.applicationContext, ViewResolver.class, true, false); if (names.length == 1) { // 添加InternalResourceViewResolver registry.getViewResolvers().add(new InternalResourceViewResolver()); } } // 視圖解析器組合 ViewResolverComposite composite = new ViewResolverComposite(); composite.setOrder(registry.getOrder()); composite.setViewResolvers(registry.getViewResolvers()); composite.setApplicationContext(this.applicationContext); composite.setServletContext(this.servletContext); return composite; }
進入到這個方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#mvcContentNegotiationManagerapp
@Bean public ContentNegotiationManager mvcContentNegotiationManager() { if (this.contentNegotiationManager == null) { // 初始化媒體類型管理器 ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext); // 設置默認類型 -》 configurer.mediaTypes(getDefaultMediaTypes()); // 配置媒體類型管理器 -》 configureContentNegotiation(configurer); // 構建媒體類型管理器 -》 this.contentNegotiationManager = configurer.buildContentNegotiationManager(); } return this.contentNegotiationManager; }
往上返回到這個方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#mvcViewResolveride
最後咱們總結下@EnableWebMvc這個註解夠幹了什麼。ui
初始化RequestMappingHandlerMappingthis
初始化視圖路徑匹配器PathMatcherurl
初始化url路徑匹配器UrlPathHelper
初始化媒體類型轉換器ContentNegotiationManager
初始化viewControllerHandlerMapping
初始化BeanNameUrlHandlerMapping
初始化resourceHandlerMapping
初始化ResourceUrlProvider
初始化defaultServletHandlerMapping
初始化RequestMappingHandlerAdapter
初始化默認請求參數解析器
初始化默認綁定參數解析器
初始化默認返回值參數解析器
初始化FormattingConversionService
初始化Validator
初始化CompositeUriComponentsContributor
初始化HttpRequestHandlerAdapter
初始化SimpleControllerHandlerAdapter
初始化HandlerExceptionResolver
初始化ViewResolver
初始化HandlerMappingIntrospector
說到最後
本次源碼解析僅表明我的觀點,僅供參考。