DispatcherServlet 源码分析(二)

来源:互联网 发布:伦铜库存数据 编辑:程序博客网 时间:2024/05/23 14:34

1. 已初始化的工具类

# Default implementation classes for DispatcherServlet's strategy interfaces.# Used as fallback when no matching beans are found in the DispatcherServlet context.# Not meant to be customized by application developers.org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolverorg.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolverorg.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingorg.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterorg.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolverorg.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslatororg.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolverorg.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager


2.处理请求的总体过程

a. 用户发送一个http请求

b. DispatcherServlet根据HandlerMapping配置找到处理请求的Handler

c.HandlerAdapter对Handler进行包装,并进行统一调用

d.Handler的返回结果会是一个ModelAndView并交由DispatcherServlet

e.DispatcherServlet将ModelAndView交由ViewResover进行解析,将逻辑视图转换成真实的视图对象View

f.DispatcherServlet将得到的View使用ModelAndView中的数据进行渲染

g.最终用户得到请求的相应


3. 处理请求整体流程

FrameworkServlet.service(HttpServletRequest request, HttpServletResponse response)

FrameworkServlet.doGet/doPost......

FrameworkServlet.processRequest(HttpServletRequest request, HttpServletResponse response)

DispatcherServlet.doService(HttpServletRequest request, HttpServletResponse response)

DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response)


4. 细节分析

a. DispatcherServlet根据HandlerMapping配置找到处理请求的Handler

step1:

DispatcherServlet

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {//遍历已经初始化好的的HandlerMapping//org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,//org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping for (HandlerMapping hm : this.handlerMappings) {......//HandlerMapping处理请求,找到合适的HandlerHandlerExecutionChain handler = hm.getHandler(request);if (handler != null) {return handler;}}return null;}



step2:

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping


private final Map<String, Object> handlerMap = new LinkedHashMap<String, Object>();@Overrideprotected Object getHandlerInternal(HttpServletRequest request) throws Exception {//这里就是http请求的Request URLString lookupPath = getUrlPathHelper().getLookupPathForRequest(request);//这里会根据已经装配的好的handlerMap来匹配请求//handlerMap会在初始化DefaultAnnotationHandlerMapping或者BeanNameUrlHandlerMapping的时候搞定,时机:ApplicationContextAware.setApplicationContext(ApplicationContext context)Object handler = lookupHandler(lookupPath, request);......return handler;}






原创粉丝点击