ContextLoaderListener监听器源代码分析

使用ContextLoaderListener也可配置SpringMVC

web.xml配置文件如下



  sun
  
    index.jsp
  
  
	
                
		contextConfigLocation
		classpath:app*.xml
	

	
		org.springframework.web.context.ContextLoaderListener
	
  
	
		DispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextAttribute
			org.springframework.web.context.WebApplicationContext.ROOT
		
		1
	
	
		DispatcherServlet
		*.do
	

进入容器加载监听器ContextLoaderListener类中,找到第107行的initWebApplicationContext方法。

进入该方法声明类ContextLoader中找到该方法,即初始化Web容器

ContextLoaderListener监听器源代码分析_第1张图片

ContextLoaderListener监听器源代码分析_第2张图片

最后初始化完毕后返回当前容器。


------------------------------------------------↓↓↓init方法片段分析↓↓↓--------------------------------------------------------


在上面两段代码片段中,其中createWebApplicationContext方法是通过反射获取wac对象,源码如下:

在配置并刷新wac容器时,调用的configureAndRefreshWebApplicationContext方法源码如下:

ContextLoaderListener监听器源代码分析_第3张图片

其中在赋值给key时,我们可以看一下ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE它的全称是什么

ContextLoaderListener监听器源代码分析_第4张图片

可以看出该key的全称是org.springframework.web.context.WebApplicationContext.ROOT


---------------------------------------↓↓↓使用拦截器配置mvc注意事项↓↓↓-------------------------------------------------


思考:为什么使用contextListener监听器配置mvc的话,我们的web.xml配置中有下面这么两行配置代码?

ContextLoaderListener监听器源代码分析_第5张图片

我们从分析DispatcherServlet类入手,

由于我们配置了标签,因此DispatcherServlet会依次执行init方法—>initServletBean()方法—>initWebApplicationContext()方法

《load-on-startup标签的作用》博客链接:load-on-startup标签的作用

源码如下:

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

 

ContextLoaderListener监听器源代码分析_第6张图片

你可能感兴趣的:(SpringMVC)