对spring的印象从DispatcherServlet开始,就从这个类开始看
从HttpServletBean开始,是spring框架自己的。
总的来说,HttpServlerBean 直接继承了java的HttpServlet,作用是将Servlet的配置的参数设置到相应的属性;
FrameworkServlet初始化了WebApplicationContext
DispatcherServlet初始化了自身的9个组件。
- HttpServletBean
init()
// Let subclasses do whatever initialization they like.
initServletBean();
由FrameworkServlet重写
@Override
protected final void initServletBean() throws ServletException {
if (this.logger.isInfoEnabled()) {
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
}
long startTime = System.currentTimeMillis();
try {
this.webApplicationContext = initWebApplicationContext(); //*
initFrameworkServlet();
}
}
进入initWebApplicationContext() (Initialize and publish the WebApplicationContext for this servlet 初始化和发布spring资源文件)
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
- 第一步,判断是不是在构造方法中传递webApplicationContext参数,ServletContext.addServlet方式注册Servlet
- 第二步,判断是不是通过web.xml配置文件传入webApplicationContext参数
- 第三步,在前面两部探索后都没有找到webApplicationContext的情况下自己创建一个。
第一步和第三步都会调用configureAndRefreshWebApplicationContext()
进入DispatherServlet.onRefresh(context)
- MultipartResplver
可以没有默认值 ,可以为null
用于上传请求 - LocalResolver
用于国际化 - ThemeResolver
主题 - HandlerMapping
处理器映射器组件 - HandlerAdapter
处理器适配器组件 - HandlerExceptionResolvers
异常处理器 - RequestToViewNameTranslator
ViewResolver根据ViewName查找View,但是有的handler处理完没有设置view,也没有设置viewname,这就是它的任务了 - ViewResolvers
视图解析器 - FlashMapManager
用来管理 FlashMap
进入doDispath()
- mappedHandler = getHandler(processedRequest);
找到对应的handler处理器,和executionChain
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
再根据处理器调用相应的适配器mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
获取ModelAndView对象,这其中包含了处理结果的视图和视图中要使用的数据render(mv, request, response);
找到一个合适的ViewResolver对mv对象进行渲染
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
- 最后给浏览器构造一个HTTP响应。
暂时这样,后续再细看!!