博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC DispatcherServlet初始化过程
阅读量:4216 次
发布时间:2019-05-26

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

先来上一张类的结构图:

图里只画了跟初始化相关的方法。

首先DispatcherServlet也是一个Servlet,初始化从init()方法开始。

下面就具体看看ini()是怎么实现的吧。

1.Servlet 是个接口;

public void init(ServletConfig config) throws ServletException;
2.GenericServlet 中实现了初始化方法。

public void init(ServletConfig config) throws ServletException {	this.config = config;	this.init();    }
这里调用了一个没有参数的init();是个空方法;

public void init() throws ServletException {    }
3.HttpServlet 没有对初始化相关的方法进行覆盖。

4.HttpServletBean,重写了init()方法。

@Override	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");		}	}
其中又掉了一个initServletBean();方法,这本类中也是个空实现。

protected void initServletBean() throws ServletException {	}
5.FrameworkServlet 果不其然的重写了上边留空的方法:initServletBean();

@Override	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 {			this.webApplicationContext = initWebApplicationContext();			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");		}	}
在这个方法中最重要的就是调用了一个initWebApplicationContext() 方法。

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;	}
一系列的赋值和判断,最关键的,跟初始化相关的,就是调用了onRefresh(),同样的套路,这个方法在本类中为空实现,留给子类去实现。

6.终于到了DispatcherServlet,找到onRefresh(),简单!调用initStrategies(ApplicationContext context);这个方法就在下边,这下清楚了,一堆的初始化方法。具体代码就不粘了。

/**	 * This implementation calls {@link #initStrategies}.	 */	@Override	protected void onRefresh(ApplicationContext context) {		initStrategies(context);	}	/**	 * Initialize the strategy objects that this servlet uses.	 * 

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

到这里整个加载过程就理清楚了。

你可能感兴趣的文章
测试工具厂商的编程语言什么时候“退休”?
查看>>
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
欢迎加入【亿能测试快讯】邮件列表!
查看>>
为什么我们的自动化测试“要”这么难
查看>>
LoadRunner性能脚本开发实战训练
查看>>
测试之途,前途?钱途?图何?
查看>>
通过FTP服务的winsockes录制脚本
查看>>
LRwinsocket协议测试AAA服务器
查看>>
Net远程管理实验
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
Software Security Testing软件安全测试
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
论文浅尝 | GMNN: Graph Markov Neural Networks
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>