博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[springMvc] 源码分析笔记(二)
阅读量:5793 次
发布时间:2019-06-18

本文共 5059 字,大约阅读时间需要 16 分钟。

1.SpringMvc 中核心Servlet的继承结构图

  

2.HttpServletBean   

public abstract class HttpServletBean extends HttpServlet		implements EnvironmentCapable, EnvironmentAware {   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");		}	}}

3. FrameworkServlet

   

public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {    protected final void initServletBean() throws ServletException {		getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");		if (this.logger.isInfoEnabled()) {			this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");		}		long startTime = System.currentTimeMillis();		try {
              //初始化webApplication this.webApplicationContext = initWebApplicationContext();               //初始化 frameworkServlet initFrameworkServlet(); } catch (ServletException ex) { this.logger.error("Context initialization failed", ex); throw ex; } catch (RuntimeException ex) { this.logger.error("Context initialization failed", ex); throw ex; } if (this.logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " + elapsedTime + " ms"); } } }

 初始化 webApplication

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;	}

 主要做了三件事

     1.获取spring的根容器rootContext
  2.设置webapplicationContext 并根据情况调用onRefresh
  3.将webApplicationContext 设置到 ServletContext

4.DispatcherServlet

public class DispatcherServlet extends FrameworkServlet {    protected void onRefresh(ApplicationContext context) {		initStrategies(context);	}       	protected void initStrategies(ApplicationContext context) {		initMultipartResolver(context);		initLocaleResolver(context);		initThemeResolver(context);		initHandlerMappings(context);		initHandlerAdapters(context);		initHandlerExceptionResolvers(context);		initRequestToViewNameTranslator(context);		initViewResolvers(context);		initFlashMapManager(context);	}}

  

 

  

 

转载于:https://www.cnblogs.com/qunan/p/7569370.html

你可能感兴趣的文章
【294天】我爱刷题系列053(2017.11.26)
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
Java虚拟机管理的内存运行时数据区域解释
查看>>
人人都会深度学习之Tensorflow基础快速入门
查看>>
ChPlayer播放器的使用
查看>>
js 经过修改改良的全浏览器支持的软键盘,随机排列
查看>>
Mysql读写分离
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
Web日志安全分析工具 v2.0发布
查看>>
统计数据库大小
查看>>
第十六章:脚本化HTTP
查看>>
EXCEL表中如何让数值变成万元或亿元
查看>>
L104
查看>>
CTOR有助于BCH石墨烯技术更上一层楼
查看>>
被遗忘的CSS
查看>>
Webpack中的sourcemap以及如何在生产和开发环境中合理的设置sourcemap的类型
查看>>
做完小程序项目、老板给我加了6k薪资~
查看>>