3.1 DispatcherServlet 初始化-initHandlerMappings/initHandlerAdapters
< 返回列表时间: 2020-07-20来源:OSCHINA
initHandlerMappings private void initHandlerMappings(ApplicationContext context) { this.handlerMappings = null; if (this.detectAllHandlerMappings) { Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerMappings = new ArrayList(matchingBeans.values()); AnnotationAwareOrderComparator.sort(this.handlerMappings); } } else { try { HandlerMapping hm = (HandlerMapping)context.getBean("handlerMapping", HandlerMapping.class); this.handlerMappings = Collections.singletonList(hm); } catch (NoSuchBeanDefinitionException var3) { ; } } if (this.handlerMappings == null) { this.handlerMappings = this.getDefaultStrategies(context, HandlerMapping.class); if (this.logger.isTraceEnabled()) { this.logger.trace("No HandlerMappings declared for servlet '" + this.getServletName() + "': using default strategies from DispatcherServlet.properties"); } } }
就是这么简单 从spring容器中获取HandlerMapping的bean 如果没有就创建一个默认的(默认文件就在mvc的jar包里 DispatcherServlet.properties) 完活 然而springboot中HandlerMapping这些是怎么注入的呢 默认的hanlerMapping实体有 0 = {PropertySourcedRequestMappingHandlerMapping@14317} 1 = {WebMvcEndpointHandlerMapping@14345} 2 = {ControllerEndpointHandlerMapping@14372} 3 = {RequestMappingHandlerMapping@14396} 4 = {BeanNameUrlHandlerMapping@15448}
initHandlerAdapters
与initHandlerMappings类似,从bean工程中获取HandlerAdapter的实现 private void initHandlerAdapters(ApplicationContext context) { this.handlerAdapters = null; if (this.detectAllHandlerAdapters) { Map<String, HandlerAdapter> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerAdapters = new ArrayList(matchingBeans.values()); AnnotationAwareOrderComparator.sort(this.handlerAdapters); } } else { try { HandlerAdapter ha = (HandlerAdapter)context.getBean("handlerAdapter", HandlerAdapter.class); this.handlerAdapters = Collections.singletonList(ha); } catch (NoSuchBeanDefinitionException var3) { ; } } if (this.handlerAdapters == null) { this.handlerAdapters = this.getDefaultStrategies(context, HandlerAdapter.class); if (this.logger.isTraceEnabled()) { this.logger.trace("No HandlerAdapters declared for servlet '" + this.getServletName() + "': using default strategies from DispatcherServlet.properties"); } } }
默认的hanlerMapping实体有 0 = {RequestMappingHandlerAdapter@6397} WebMvcAutoConfiguration 1 = {HttpRequestHandlerAdapter@6398} WebMvcConfigurationSupport 2 = {SimpleControllerHandlerAdapter@6399} WebMvcConfigurationSupport
热门排行