`
zhangwei_david
  • 浏览: 468799 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring MVC之HandlerMap 初始化(RequestmappingHandlerMapping)

阅读更多

    AbstractHandlerMethodMapping类实现了InitializingBean接口,在属性初始化完成后会调用afterPropertiesSet()方法,在该方法中调用initHandlerMethods();进行HandlerMethod初始化。

   

/**
	 * 扫描ApplicationContext中的Bean,查找并注册 handlerMethod
	 */
	protected void initHandlerMethods() {
		if (logger.isDebugEnabled()) {
			logger.debug("Looking for request mappings in application context: " + getApplicationContext());
		}
		//从ApplicationContext中获取所有Bean名称
		String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
				getApplicationContext().getBeanNamesForType(Object.class));
		//遍历Bean
		for (String name : beanNames) {
			if (!name.startsWith(SCOPED_TARGET_NAME_PREFIX) && isHandler(getApplicationContext().getType(name))) {
				//从bean中查找handler method
				detectHandlerMethods(name);
			}
		}
		//handler method 初始化
		handlerMethodsInitialized(getHandlerMethods());
	}

 

/**
	 * 从一个Handler中查找 handler method
	 */
	protected void detectHandlerMethods(final Object handler) {
		Class<?> handlerType =
				(handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass());

		
		final Map<Method, T> mappings = new IdentityHashMap<Method, T>();
		//获取Class
		final Class<?> userType = ClassUtils.getUserClass(handlerType);

		Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
			@Override
			public boolean matches(Method method) {
				T mapping = getMappingForMethod(method, userType);
				if (mapping != null) {
					mappings.put(method, mapping);
					return true;
				}
				else {
					return false;
				}
			}
		});
		//遍历所有handler method,并注册
		for (Method method : methods) {
			registerHandlerMethod(handler, method, mappings.get(method));
		}
	}

 isHandler 在RequestMappingHandlerMapping 具体实现

	/**
	 * {@inheritDoc}
	 * Expects a handler to have a type-level @{@link Controller} annotation.
	 */
	@Override
	protected boolean isHandler(Class<?> beanType) {
		return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
				(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
	}

 

0
0
分享到:
评论

相关推荐

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    Spring MVC启动时初始化的几个常用方法

    NULL 博文链接:https://bijian1013.iteye.com/blog/2296743

    spring mvc项目

    可用于分析spring mvc源码、spring mvc父子容器初始化流程、session和cookie机制、spring session等,也可以用于学习Java Web(servlet、filter、listener等)、spring源码等。 该项目使用servlet3.0规范,无web.xml...

    spring mvc 官方文档

    本文详细介绍spring MVC的原理和开发心得体会。

    精通Spring MVC 4 中文

    精通Spring MVC 4 中文

    Spring MVC 基于注解实例

    Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...

    [免费]Spring MVC学习指南(高清)

    全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...

    Spring mvc 国际化

    Spring MVC 国际化的配置方法及使用,希望能对大家的学习、工作有帮助。

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

    《Spring MVC+MyBatis开发从入门到项目实战》分为4篇。第1篇是Java开发环境的搭建,包括JDK的下载与安装、环境变量的配置、MyEclipse的下载与基本配置。第2篇是MyBatis技术入门,包括剖析JDBC的弊端、MyBatis的背景...

    Spring MVC所需jar包

    Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包

    大优惠 Spring MVC学习指南(第2版)2017.pdf

    全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...

    Spring MVC jar包

    关于构建Sping MVC的Jar包,包括Sping2.5.6和Hibernate3.6.8

    spring MVC数据绑定大全

    spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...

    Spring MVC 入门实例

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...

    Servlet JSP和Spring MVC初学指南

    Servlet JSP和Spring MVC初学指南

    Spring MVC 4.2.3

    Spring mvc jar包

    spring mvc源代码

    spring mvc4.1.4 源代码 spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码

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

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

Global site tag (gtag.js) - Google Analytics