spring mvc源码分析

1.传统xml配置方式

web.xml里面配置:org.springframework.web.servlet.DispatcherServlet,处理项目的spring配置文件:classpath*:wly-servlet.xml

2.DispatcherServlet

## doService
	暴露DispatcherServlet中从request获取的属性,并且委派给doDispatch处理

## doDispatch
	## step1:
	// Determine handler for the current request.
	mappedHandler = getHandler(processedRequest);

3.wly-servlet.xml

在这个配置文件里面配置了很多由spring负责创建的bean实例:包括controller和servlet类
## <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
	```
		处理bean.name和url映射关系,将url交给
		//handlerName:"/wang" 
		//resolvedHandler:com.wang.controller.WangController@4e2c46ed
		resolvedHandler = getApplicationContext().getBean(handlerName);
		...
		this.handlerMap.put(urlPath, resolvedHandler);//其实就是把url和bean的映射关系放到了一个map里面
	```
##<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
	```	
		handler包装为适配器
		//根据当前handler获取handlerexecuteChain
		
		
		//根据handler的类型获取handler的适配器
		protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
			for (HandlerAdapter ha : this.handlerAdapters) {
				if (logger.isTraceEnabled()) {
					logger.trace("Testing handler adapter [" + ha + "]");
				}
				//根据handler instanceof Controller判断使用什么适配器类代替当前handler
				if (ha.supports(handler)) {
					return ha;
				}
			}
			throw new ServletException("No adapter for handler [" + handler +
					"]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
		}
		
		。。。
		//ha.supports(handler)方法
			@Override
			public boolean supports(Object handler) {
				return (handler instanceof Controller);
			}
		
		// 实际调用对应handler的方法
		mv = ha.handle(processedRequest, response, mappedHandler.getHandler());	
		
		//执行完具体的handler方法后,通过拦截器处理视图信息:
		applyDefaultViewName(processedRequest, mv);//设置modelview对象
		//通过
		mappedHandler.applyPostHandle(processedRequest, response, mv);
		[org.springframework.web.servlet.handler.AbstractUrlHandlerMapping$PathExposingHandlerInterceptor@46dafc9f, 
		org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor@1faa1cba]
	```
	

## <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
视图处理器,将逻辑视图转转成物理视图

posted on 2018-12-28 09:21  lukelin1989  阅读(153)  评论(0编辑  收藏  举报

导航