Spring MVC 原理解析「终于解决」

Spring MVC 原理解析「终于解决」XXVERsion.jar里的spring.handlers文件里标明解析方式http\://www.springframework.org/s

欢迎大家来到IT世界,在知识的湖畔探索吧!

springMVC框架原理解析

整体工作原理

Spring MVC 原理解析「终于解决」

XML方式

spring-mvc:XXVERsion.jar里的spring.handlers文件里标明解析方式

http\://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler

欢迎大家来到IT世界,在知识的湖畔探索吧!

使用RequestMappingHandlerMapping来对mvc的注解进行扫描

欢迎大家来到IT世界,在知识的湖畔探索吧!public class MvcNamespaceHandler extends NamespaceHandlerSupport {
 @Override
 public void init() {
 //这里注册使用RequestMappingHandlerMapping来进行MVC的处理
 registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
 registerBeanDefinitionParser("default-servlet-handler", new DefaultServletHandlerBeanDefinitionParser());
 registerBeanDefinitionParser("interceptors", new InterceptorsBeanDefinitionParser());
 registerBeanDefinitionParser("resources", new ResourcesBeanDefinitionParser());
 registerBeanDefinitionParser("view-controller", new ViewControllerBeanDefinitionParser());
 registerBeanDefinitionParser("redirect-view-controller", new ViewControllerBeanDefinitionParser());
 registerBeanDefinitionParser("status-controller", new ViewControllerBeanDefinitionParser());
 registerBeanDefinitionParser("view-resolvers", new ViewResolversBeanDefinitionParser());
 registerBeanDefinitionParser("tiles-configurer", new TilesConfigurerBeanDefinitionParser());
 registerBeanDefinitionParser("freemarker-configurer", new FreeMarkerConfigurerBeanDefinitionParser());
 registerBeanDefinitionParser("velocity-configurer", new VelocityConfigurerBeanDefinitionParser());
 registerBeanDefinitionParser("groovy-configurer", new GroovyMarkupConfigurerBeanDefinitionParser());
 registerBeanDefinitionParser("script-template-configurer", new ScriptTemplateConfigurerBeanDefinitionParser());
 registerBeanDefinitionParser("cors", new CorsBeanDefinitionParser());
 }
}

注解

注解形式的SpringMvc使用@EnableWebMvc

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

DelegatingWebMvcConfiguration是一个Configuration形式的配置,里面以@Bean的方式注册了RequestMappingHandlerMapping

看下源码:

WebMvcConfigurationSupport是DelegatingWebMvcConfiguration的父类

欢迎大家来到IT世界,在知识的湖畔探索吧!public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
 //向容器注入RequestMappingHandlerMapping
 @Bean
 public RequestMappingHandlerMapping requestMappingHandlerMapping() {
 RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
 mapping.setOrder(0);
 mapping.setInterceptors(getInterceptors());
 mapping.setContentNegotiationManager(mvcContentNegotiationManager());
 mapping.setCorsConfigurations(getCorsConfigurations());
 PathMatchConfigurer configurer = getPathMatchConfigurer();
 if (configurer.isUseSuffixPatternMatch() != null) {
 mapping.setUseSuffixPatternMatch(configurer.isUseSuffixPatternMatch());
 }
 if (configurer.isUseRegisteredSuffixPatternMatch() != null) {
 mapping.setUseRegisteredSuffixPatternMatch(configurer.isUseRegisteredSuffixPatternMatch());
 }
 if (configurer.isUseTrailingSlashMatch() != null) {
 mapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
 }
 UrlPathHelper pathHelper = configurer.getUrlPathHelper();
 if (pathHelper != null) {
 mapping.setUrlPathHelper(pathHelper);
 }
 PathMatcher pathMatcher = configurer.getPathMatcher();
 if (pathMatcher != null) {
 mapping.setPathMatcher(pathMatcher);
 }
 return mapping;
 }
}

DispatcherServlet处理请求

Spring MVC 原理解析「终于解决」

public class DispatcherServlet extends FrameworkServlet {
 /** List of HandlerMappings used by this servlet */
 private List<HandlerMapping> handlerMappings;
 /** List of HandlerAdapters used by this servlet */
 private List<HandlerAdapter> handlerAdapters;
 protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
 //获取RequestMappingHandlerMapping,然后再获取HandlerExecutionChain
 HandlerExecutionChain mappedHandler = getHandler(processedRequest);
 //获取RequestMappingHandlerAdapter,真正处理请求的逻辑执行者
 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
 //执行
 mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
 applyDefaultViewName(processedRequest, mv);
 mappedHandler.applyPostHandle(processedRequest, response, mv);
 }
 }
}

下面再来看几个重要的类

HandlerMapping

AnnotationDrivenBeanDefinitionParser解析xml注解会向BeanFactory里注册内置的HandlerMapping

  • SimpleUrlHandlerMapping
  • RequestMappingHandlerMapping
  • BeanNameUrlHandlerMapping
  • WebMvcConfigurationSupport.EmptyHandlerMapping
public BeanDefinition parse(Element element, ParserContext parserContext) {
//...省略其他代码
//注册最终要的RequestMappingHandlerMapping到容器中
parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, HANDLER_MAPPING_BEAN_NAME));
 parserContext.registerComponent(new BeanComponentDefinition(handlerAdapterDef, HANDLER_ADAPTER_BEAN_NAME));
//注册默认的HandlerMapping
MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
}
}
  • 其中最终要的就是RequestMappingHandlerMapping了。

RequestMappingHandlerMapping

RequestMappingHandlerMapping是用来处理SpringMVC注解(@Controller、@RequestMapping)的核心类

RequestMappingHandlerMapping会扫描所有@Controller和@RequestMapping,将处理请求的方法封装为HandlerMethod,存储在MappingRegistry里。

而MappingRegistry则是维护了一个url和对应处理方法的Map,Map> nameLookup

具体的逻辑可查看 RequestMappingHandlerMapping父类AbstractHandlerMethodMapping的afterPropertiesSet方法,这里会做前面的初始化工作

HandlerExecutionChain

HandlerExecutionChain的结构

  • handler:HandlerMethod
  • bean 实际的controller
  • method url请求对应的method
  • parameters 方法对应的参数
  • interceptors:HandlerInterceptor[]

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/22845.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信