Spring-DispatcherServlet初始化详解:[url]http://donald-draper.iteye.com/blog/2325394[/url]
Spring的DefaultAnnotationHandlerMapping详解:[url]http://donald-draper.iteye.com/blog/2325453[/url]
前面我们已经探究过DispatcherServlet的初始化,下面我们来看看
DispatcherServlet如何处理web请求
//DispatcherServlet
从DispatcherServlet我们看不到任何请求处理入口再来看FrameworkServlet
我们来看DispatcherServlet
[color=blue]总结:
DispatcherServlet处理请求实际上是通过FrameworkServlet的Service()和do*类方法,
而这些方式实际的处理逻辑在DispatcherServlet的doService和doDispatch中,doDispatch
先找出控制器处理器,然后再找出控制器方法适配器,通过控制器方法适配器,来处理具体请求。[/color]
Spring的DefaultAnnotationHandlerMapping详解:[url]http://donald-draper.iteye.com/blog/2325453[/url]
前面我们已经探究过DispatcherServlet的初始化,下面我们来看看
DispatcherServlet如何处理web请求
//DispatcherServlet
public class DispatcherServlet extends FrameworkServlet
{
}
从DispatcherServlet我们看不到任何请求处理入口再来看FrameworkServlet
public abstract class FrameworkServlet extends HttpServletBean
implements ApplicationContextAware
{
public FrameworkServlet(WebApplicationContext webApplicationContext)
{
contextClass = DEFAULT_CONTEXT_CLASS;
contextInitializers = new ArrayList();
publishContext = true;
publishEvents = true;
threadContextInheritable = false;
dispatchOptionsRequest = false;
dispatchTraceRequest = false;
webApplicationContextInjected = false;
refreshEventReceived = false;
this.webApplicationContext = webApplicationContext;
}
//在这里我们发现了处理请求的方法,默认方法
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String method = request.getMethod();
if(method.equalsIgnoreCase(RequestMethod.PATCH.name()))
processRequest(request, response);
else
super.service(request, response);
}
//GET
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
//POST
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
//PUT
protected final void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
//DELETE
protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
//实际处理请求方法
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
//处理请求
doService(request, response);
}
}
//带子类DispatcherServlet扩展
protected abstract void doService(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws Exception;
public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";
public static final Class DEFAULT_CONTEXT_CLASS = org/springframework/web/context/support/XmlWebApplicationContext;
public static final String SERVLET_CONTEXT_PREFIX = (new StringBuilder()).append(org/springframework/web/servlet/FrameworkServlet.getName()).append(".CONTEXT.").toString();
private static final String INIT_PARAM_DELIMITERS = ",; \t\n";
private String contextAttribute;
private Class contextClass;
private String contextId;
private String namespace;
private String contextConfigLocation;
private final ArrayList contextInitializers;
private String contextInitializerClasses;
private boolean publishContext;
private boolean publishEvents;
private boolean threadContextInheritable;
private boolean dispatchOptionsRequest;
private boolean dispatchTraceRequest;
private WebApplicationContext webApplicationContext;
private boolean webApplicationContextInjected;
private boolean refreshEventReceived;
}
我们来看DispatcherServlet
public class DispatcherServlet extends FrameworkServlet
{
public DispatcherServlet(WebApplicationContext webApplicationContext)
{
super(webApplicationContext);
detectAllHandlerMappings = true;
detectAllHandlerAdapters = true;
detectAllHandlerExceptionResolvers = true;
detectAllViewResolvers = true;
throwExceptionIfNoHandlerFound = false;
cleanupAfterInclude = true;
}
//处理请求
protected void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
//设置请求属性,web上下文,本地化,主题
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
request.setAttribute(THEME_RESOLVER_ATTRIBUTE, themeResolver);
request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());
FlashMap inputFlashMap = flashMapManager.retrieveAndUpdate(request, response);
if(inputFlashMap != null)
request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, flashMapManager);
//委托给doDispatch
doDispatch(request, response);
}
protected void doDispatch(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
HttpServletRequest processedRequest;
HandlerExecutionChain mappedHandler;
boolean multipartRequestParsed;
WebAsyncManager asyncManager;
processedRequest = request;
mappedHandler = null;
multipartRequestParsed = false;
asyncManager = WebAsyncUtils.getAsyncManager(request);
ModelAndView mv;
Exception dispatchException;
mv = null;
dispatchException = null;
processedRequest = checkMultipart(request);
multipartRequestParsed = processedRequest != request;
//获取控制器处理器链
mappedHandler = getHandler(processedRequest);
HandlerAdapter ha;
//获取控制器方法适配器
ha = getHandlerAdapter(mappedHandler.getHandler());
//获取处理视图
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//返回视图
applyDefaultViewName(request, mv);
}
//获取控制器处理器链
protected HandlerExecutionChain getHandler(HttpServletRequest request)
throws Exception
{
//List<HashMap<String,HandlerMapping>>
for(Iterator iterator = handlerMappings.iterator(); iterator.hasNext();)
{
HandlerMapping hm = (HandlerMapping)iterator.next();
if(logger.isTraceEnabled())
logger.trace((new StringBuilder()).append("Testing handler map [").append(hm).append("] in DispatcherServlet with name '").append(getServletName()).append("'").toString());
HandlerExecutionChain handler = hm.getHandler(request);
if(handler != null)
return handler;
}
}
//获取控制器方法适配器
protected HandlerAdapter getHandlerAdapter(Object handler)
throws ServletException
{
//List<HashMap<String,HandlerAdapter>>
for(Iterator iterator = handlerAdapters.iterator(); iterator.hasNext();)
{
HandlerAdapter ha = (HandlerAdapter)iterator.next();
if(logger.isTraceEnabled())
logger.trace((new StringBuilder()).append("Testing handler adapter [").append(ha).append("]").toString());
if(ha.supports(handler))
return ha;
}
}
private List handlerMappings;//List<HashMap<String,HandlerMapping>>,Key为beanName,value值为HandlerMapping实例
private List handlerAdapters;//List<HashMap<String,HandlerAdapter>>,Key为beanName,value值为HandlerAdapter实例
private List handlerExceptionResolvers;
private List viewResolvers;
static
{
try
{
//加载默认配置文件
ClassPathResource resource = new ClassPathResource("DispatcherServlet.properties", org/springframework/web/servlet/DispatcherServlet);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
}
}
[color=blue]总结:
DispatcherServlet处理请求实际上是通过FrameworkServlet的Service()和do*类方法,
而这些方式实际的处理逻辑在DispatcherServlet的doService和doDispatch中,doDispatch
先找出控制器处理器,然后再找出控制器方法适配器,通过控制器方法适配器,来处理具体请求。[/color]