`
fancyboy2050
  • 浏览: 238553 次
  • 性别: Icon_minigender_1
  • 来自: 皇城根儿下
社区版块
存档分类
最新评论

spring mvc请求映射关系处理

阅读更多
1:看DispatcherServlet的初始化策略
protected void initStrategies(ApplicationContext context) {
		initMultipartResolver(context);
		initLocaleResolver(context);
		initThemeResolver(context);
//初始化处理器映射关系,即用户请求与程序处理的对应关系
		initHandlerMappings(context);
		initHandlerAdapters(context);
		initHandlerExceptionResolvers(context);
		initRequestToViewNameTranslator(context);
		initViewResolvers(context);
	}

//initHandlerMappings默认会先探测ApplicationContext对象中已经设定好的任何HandlerMapping对象,如果有就使用定义好的,如果没有,调用方法getDefaultStrategies,使用默认配置,DisptatchServlet.properties文件中默认的org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping两个HandlerMapping对象。


2:看下DefaultAnnotationHandlerMapping类层次关系,BeanNameUrlHandlerMapping类层次是一样的
//类层次,凑合看
DefaultAnnotationHandlerMapping
	AbstractDetectingUrlHandlerMapping
		AbstractUrlHandlerMapping
			AbstractHandlerMapping
				extends WebApplicationObjectSupport implements HandlerMapping, Ordered

//这里继承了WebApplicationObjectSupport对象,关于这个对象:Convenient superclass for application objects running in a WebApplicationContext.
WebApplicationObjectSupport
	extends ApplicationObjectSupport implements ServletContextAware
		implements ApplicationContextAware


3:抽象类ApplicationObjectSupport实现了ApplicationContextAware,spring容器的后置处理器会调用setApplicationContext方法,执行顺序:
//ApplicationObjectSupport中的setApplicationContext方法执行
public final void setApplicationContext(ApplicationContext context) throws BeansException {
		if (context == null && !isContextRequired()) {
			// Reset internal context state.
			this.applicationContext = null;
			this.messageSourceAccessor = null;
		}
		else if (this.applicationContext == null) {
			// Initialize with passed-in context.
			if (!requiredContextClass().isInstance(context)) {
				throw new ApplicationContextException(
						"Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
			}
			this.applicationContext = context;
			this.messageSourceAccessor = new MessageSourceAccessor(context);
//这里会先执行子类WebApplicationObjectSupport的initApplicationContext方法,然后到ApplicationObjectSupport自己的init方法,ApplicationObjectSupport本身的该方法没有任何处理,只是调用了一个空的方法initApplicationContext(),这个无参的重载方法被当作一个钩子供子类方法来实现。
			initApplicationContext(context);
		}
		else {
			// Ignore reinitialization if same context passed in.
			if (this.applicationContext != context) {
				throw new ApplicationContextException(
						"Cannot reinitialize with different application context: current one is [" +
						this.applicationContext + "], passed-in one is [" + context + "]");
			}
		}
	}


4:initApplicationContext()执行,spring默认的两个处理器映射类都继承自AbstractDetectingUrlHandlerMapping抽象类,而且类初始化将被执行的initApplicationContext方法也在这个类得到了实现
//AbstractDetectingUrlHandlerMapping中的initApplicationContext方法
public void initApplicationContext() throws ApplicationContextException {
//先执行超类的initApplicationContext方法,这个超类的方法完成的任务就是对定义好的拦截器改装并放入到adaptedInterceptors数组中供以后使用
		super.initApplicationContext();
//请求与处理器映射的关键方法
		detectHandlers();
	}


5:detectHandlers()执行
protected void detectHandlers() throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
		}
//默认会取得上下文中的所有的对象的beanname。
		String[] beanNames = (this.detectHandlersInAncestorContexts ?
				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
				getApplicationContext().getBeanNamesForType(Object.class));

		// Take any bean name that we can determine URLs for.
		for (int i = 0; i < beanNames.length; i++) {
			String beanName = beanNames[i];
//abstract方法,在子类中实现,即找到符合要求的映射url
			String[] urls = determineUrlsForHandler(beanName);
			if (!ObjectUtils.isEmpty(urls)) {
				// URL paths found: Let's consider it a handler.
//注册处理器,即添加一些映射关系到handlerMap中,一个LinkedHashMap
				registerHandler(urls, beanName);
			}
			else {
				if (logger.isDebugEnabled()) {
					logger.debug("Rejected bean name '" + beanNames[i] + "': no URL paths identified");
				}
			}
		}
	}


6:determineUrlsForHandler方法执行,DefaultAnnotationHandlerMapping为例子
不贴了,主要就是挨个对象查看,DefaultAnnotationHandlerMapping类就是查看定义的RequestMapping注解,然后把符合要求的urls返回。
分享到:
评论

相关推荐

    精通Spring MVC 4

    本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、优化请求、将Web应用部署到云等内容,循序渐进地讲解了Spring MVC4...

    Spring MVC请求映射常见的三种方式

    NULL 博文链接:https://xieke90.iteye.com/blog/2235706

    Spring MVC入门教程

    八、spring mvc 请求如何映射到具体的Action中的方法? 九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? ...

    Spring MVC 教程 快速入门 深入分析

    八、spring mvc 请求如何映射到具体的Action中的方法? 九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? ...

    Spring MVC+MyBatis开发从入门到项目实战

    第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...

    SpringMVC框架架构介绍

    八、spring mvc 请求如何映射到具体的Action中的方法? 九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? ...

    Spring MVC 入门实例

    然后我们再配置 Servlet 映射(servlet-mapping 标签), 也就是你希望哪些请求被DispatcherServlet处理. 这里, 我们设置后缀名为 do(*.do) 的所有URL请求都被名为 ideawu 的 DispatcherServlet 的程序处理. 选择 .do ...

    第5章spring-mvc请求映射处理

    RequestMapping测试案例

    SPRING MVC 的请求参数获取的几种方法

    慨括了spring mvc请求参数常见的几种获取方法

    Spring MVC 3.0实战指南.ppt

    HTTP请求映射原理 Spring MVC进行映射的依据 通过URL限定:URL表达式 通过URL限定:绑定{xxx}中的值 通过请求方法限定:请求方法 通过请求方法限定:代码示例 通过请求方法限定:模拟请求方法 通过请求/请求头参数限定:...

    Spring MVC--2.@RequestMapping 映射请求

    Spring MVC--2.@RequestMapping 映射请求

    全面掌握Spring MVC:从基础到高级的实践指南

    Spring MVC是Spring框架的一个模块,专注于构建Web应用程序。作为架构师和Java开发者,深入理解Spring MVC的原理和实践应用...例如,通过使用@Controller和@RequestMapping注解,开发者可以轻松定义控制器和请求映射。

    Spring MVC的教程项目代码

    奉上学习Spring MVC 3.0的技术资料PPT资料,我花了两天写成,该资料根据最新拙作的《Spring 3.x 企业应用开发实战 》写成,仅需要1个小时左右的时间,就可以让你学习到Spring MVC 3.0的所有知识点和新功能,强烈推荐...

    精通Spring MVC 4 [精校高清版](Geoffroy.Warin). pdf

    本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、优化请求、将Web应用部署到云等内容,循序渐进地讲解了Spring MVC4...

    Spring3MVC注解教程.ppt

    HTTP请求映射原理 Spring MVC进行映射的依据 通过URL限定:URL表达式 通过URL限定:绑定{xxx}中的值 通过请求方法限定:请求方法 通过请求方法限定:代码示例 通过请求方法限定:模拟请求方法 通过请求/请求头参数...

    spring mvc 3.2 参考文档

    Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...

    Spring Web MVC入门教程

    第一章:Spring Web MVC入门 包括:是什么、能干什么、有什么、各个组成部分的功能、HelloWorld等 第二章:理解DispatcherServlet 包括:功能、配置、上下文关系、初始化顺序等 第三章:注解式控制器开发详解 ...

    使用Spring MVC和JSP构建的全栈RSS阅读器Web应用程序(95分以上课程大作业).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

Global site tag (gtag.js) - Google Analytics