DispatcherServlet生命周期
tomcat启动初始化DispatcherServlet 1.加载默认DispatcherServlet.properties配置,加载properties配置中的策略;ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);de...
tomcat启动初始化DispatcherServlet
1.加载默认DispatcherServlet.properties配置,加载properties配置中的策略;
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
2.DispatcherServlet.DispatcherServlet()->FrameworkServlet.init()。DispatcherServlet执行父类FrameworkServlet的init()方法,FrameworkServlet的init()方法,继承自HttpServletBean。init()的作用是将配置参数映射到servlet的bean属性上,并调用配置参数的子类(例如:org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver、org.springframework.web.servlet.theme.FixedThemeResolver、org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping等
)初始化,然后将WebApplicationContext也初始化一下,WebApplicationContext初始化的时候会去做一个操作,根据ApplicationContext重新初始化策略。DispatcherServlet初始化结束,等待客户端请求。
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}
// Set bean properties from init parameters.
try {
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
throw ex;
}
// Let subclasses do whatever initialization they like.
initServletBean();
if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
/**
* Initialize the strategy objects that this servlet uses.
* <p>May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
客户端请求DispatcherServlet。
1.客户端请求进来调用继承自DispatcherServlet的父类FrameworkServlet的service()方法,service()方法调用继承自父类HttpServlet的service()方法。service()方法根据请求头调用doGet()/doPost()(继承自父类FrameworkServlet)等方法。doGet()/doGet等方法调用processRequest()处理请求;
2.processRequest()先构建LocaleContext、构建请求的Attributes;
3.然后processRequest()调用DispatcherServlet的doService();
4.doService调用doDispatch,根据action分发请求,响应结果response给客户端。
等待下一次请求。。。。
值得细说的是doService调用doDispatch
1.判断请求是否multipart request,如果是则用Multipart方式解析request;
2.检查当前请求handlerMapping及handlerAdapter;
3.处理请求头;
4.调用程序处理请求,处理完返回ModelAndView,设置视图名称;
5.处理程序选择和处理程序调用的结果,要么返回ModelAndView,要么返回ModelAndView的异常;
6.结束doDispatch。
更多推荐
所有评论(0)