Spring源码学习(二)ContextLoaderListener

3 篇文章 0 订阅

我们可以看到,一般SpringMVC的项目,在其web.xml文件中都会配置这样一段:

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

启用ContextLoaderListener监听器,先来看看它的作用

/**
 * Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
 * Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
 *
 * <p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener}
 * in {@code web.xml}, if the latter is used.
 *
 * <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
 * application context via the {@link #ContextLoaderListener(WebApplicationContext)}
 * constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
 * See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 17.02.2003
 * @see #setContextInitializers
 * @see org.springframework.web.WebApplicationInitializer
 * @see org.springframework.web.util.Log4jConfigListener
 */
public class ContextLoaderListener extends ContextLoader implements ServletContextListener

看注释,这是一个引导的Listener,用来开始和关闭Spring的根上下文。它是委托给ContextLoader类对象来处理开始和关闭的。并且如果要用到Log4jConfigListener的话,要把ContextLoaderListener注册在其后面。 Spring 3.1后,ContextLoaderListener支持通过带WebApplicationContext参数的构造函数去注入根web应用上下文,这种方式可以实现可编程的配置。

我的理解,这个类就是用来加载应用程序的上下文,也就是那些配置项、bean之类的数据,而且是根数据,可以被整个应用程序共享的。当然了,它的创建需要在ServletContext这个总上下文被初始化后,所以要实现ServletContextListener监听接口,等待contextInitialized这个事件,并接收到ServletContext对象。而它加载的WebApplicationContext对象也会被保存在ServletContext对象中,这样才能让整个应用程序共享。

看下它都构造函数,ContextLoaderListener有带参数和不带参数的两个构造函数: 

<span style="white-space:pre">	</span>/**
	 * Create a new {@code ContextLoaderListener} that will create a web application
	 * context based on the "contextClass" and "contextConfigLocation" servlet
	 * context-params. See {@link ContextLoader} superclass documentation for details on
	 * default values for each.
	 * <p>This constructor is typically used when declaring {@code ContextLoaderListener}
	 * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is
	 * required.
	 * <p>The created application context will be registered into the ServletContext under
	 * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
	 * and the Spring application context will be closed when the {@link #contextDestroyed}
	 * lifecycle method is invoked on this listener.
	 * @see ContextLoader
	 * @see #ContextLoaderListener(WebApplicationContext)
	 * @see #contextInitialized(ServletContextEvent)
	 * @see #contextDestroyed(ServletContextEvent)
	 */
	public ContextLoaderListener() {
	}

	/**
	 * Create a new {@code ContextLoaderListener} with the given application context. This
	 * constructor is useful in Servlet 3.0+ environments where instance-based
	 * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener}
	 * API.
	 * <p>The context may or may not yet be {@linkplain
	 * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it
	 * (a) is an implementation of {@link ConfigurableWebApplicationContext} and
	 * (b) has <strong>not</strong> already been refreshed (the recommended approach),
	 * then the following will occur:
	 * <ul>
	 * <li>If the given context has not already been assigned an {@linkplain
	 * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li>
	 * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to
	 * the application context</li>
	 * <li>{@link #customizeContext} will be called</li>
	 * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}s
	 * specified through the "contextInitializerClasses" init-param will be applied.</li>
	 * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li>
	 * </ul>
	 * If the context has already been refreshed or does not implement
	 * {@code ConfigurableWebApplicationContext}, none of the above will occur under the
	 * assumption that the user has performed these actions (or not) per his or her
	 * specific needs.
	 * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
	 * <p>In any case, the given application context will be registered into the
	 * ServletContext under the attribute name {@link
	 * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring
	 * application context will be closed when the {@link #contextDestroyed} lifecycle
	 * method is invoked on this listener.
	 * @param context the application context to manage
	 * @see #contextInitialized(ServletContextEvent)
	 * @see #contextDestroyed(ServletContextEvent)
	 */
	public ContextLoaderListener(WebApplicationContext context) {
		super(context);
	}


先来看看无参数的构造函数,创建一个ContextLoaderListener对象,用来创建新的web应用上下文。创建过程会基于 "contextClass" 和"contextConfigLocation" 两个servlet

 context-params参数(ServletContext对象初始化的时候会加载 context-params内的参数),详情看ContextLoader这个类。这种构造函数应用于通过web.xml声明ContextLoaderListener监听器。创建的应用程序上下文会被注册到ServletContext中的 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性(无论哪种构造函数,最后创建的对象都会注册到这个属性)。当contextDestroyed生命周期方法被调用后,此应用程序上下文将会关闭。

我的理解,我们通常的做法即通过web.xml文件来配置的方式,会调用这个无参的构造函数。可以配置 "contextClass" 和"contextConfigLocation" 两个servlet context-params参数,而默认的参数值在ContextLoader这个父类里(不配置会使用ContextLoader类内部定义的默认值)。


<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
          classpath:spring-common-config.xml,
          classpath:spring-budget-config.xml
      </param-value>
</context-param>
<listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


配置参数: 

参数

描述

contextClass

实现WebApplicationContext接口的类,当前的servlet用它来创建上下文。如果这个参数没有指定, 默认使用XmlWebApplicationContext。

contextConfigLocation

传给上下文实例(由contextClass指定)的字符串,用来指定上下文的位置。这个字符串可以被分成多个字符串(使用逗号作为分隔符) 来支持多个上下文(在多上下文的情况下,如果同一个bean被定义两次,后面一个优先)。

namespace

WebApplicationContext命名空间。默认值是[server-name]-servlet。

 


再看看带WebApplicationContext参数的构造函数,创建一个ContextLoaderListener对象,赋值一个给定的应用程序上下文。在Servlet 3.0+环境下,这个构造函数会很有用,可以使用 javax.servlet.ServletContext.addListener(String className) API实现基于实例的监听器注册。

这个给定的上下文可能被刷新(org.springframework.context.ConfigurableApplicationContext接口的refresh方法,无论是否是Web类的Spring项目最后都是通过继承关系由AbstractApplicationContext虚类实现该接口的refresh方法,区别在于后来调用的loadBeanDefinitions方法的实现不同。刷新的过程就是读取spring配置文件,注册里面的bean)也可能没有:

一、如果它实现了ConfigurableWebApplicationContext接口,并且还没有被刷新(推荐的方式),则:

1、如果上下文还没有分配id(org.springframework.context.ConfigurableApplicationContext.setId(String id)方法),则分配一个id。

2、ServletContextServletConfig 对象会被委托给此上下文。

3、customizeContext被调用。

4、任意通过 "contextInitializerClasses" init-param 初始化参数指定的ApplicationContextInitializer将会被应用。

5、调用org.springframework.context.ConfigurableApplicationContext.refresh()方法。

二、如果它已经被刷新了,或者没有实现ConfigurableWebApplicationContext接口

则上面的步骤都不会执行,基于的假设是用户已经根据需求决定是否执行或者已经执行过了(就是不做任何操作,没实现该接口就无法从配置文件读取并加载bean,已刷新表明bean已被读取并注册到上下文中)。

无论什么情况,给定的上下文都会被注册到ServletContext中的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性。

说说javax.servlet.ServletContext.addListener(String className)方法,用来添加监听器,传入参数是监听器类型。通过ServletContext关联的classloader加载传入类型。此类必须至少实现一个以下接口:

•ServletContextAttributeListener 
•ServletRequestListener 
•ServletRequestAttributeListener 
•javax.servlet.http.HttpSessionListener 
•javax.servlet.http.HttpSessionAttributeListener 



 我的理解,通过程序创建ContextLoaderListener对象并传入上下文对象时使用这个构造函数,可以动态加载应用程序上下文。

再来看看初始化上下文的方法:

	/**
	 * Initialize the root web application context.
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}
实际是通过initWebApplicationContext方法来做初始化的。此方法由父类ContextLoader定义。它会先检查应用程序上下文是否已经初始化过,即如果WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性不为空,不做操作,抛出异常返回。否则开始初始化过程:先判断context属性是否为空,为空创建一个WebApplicationContext对象(创建过程就是读取web.xml中配置的contextClass属性创建该类型对象,详见上面配置参数),这种情况匹配ContextLoaderListener的无参构造函数。不为空,匹配ContextLoaderListener的带参构造函数。正如开始设想的,初始化完毕的WebApplicationContext对象保存到ServletContext对象中,以便让整个应用程序共享。

/**
	 * Initialize Spring's web application context for the given servlet context,
	 * using the application context provided at construction time, or creating a new one
	 * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
	 * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
	 * @param servletContext current servlet context
	 * @return the new WebApplicationContext
	 * @see #ContextLoader(WebApplicationContext)
	 * @see #CONTEXT_CLASS_PARAM
	 * @see #CONFIG_LOCATION_PARAM
	 */
	public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
			throw new IllegalStateException(
					"Cannot initialize context because there is already a root application context present - " +
					"check whether you have multiple ContextLoader* definitions in your web.xml!");
		}

		Log logger = LogFactory.getLog(ContextLoader.class);
		servletContext.log("Initializing Spring root WebApplicationContext");
		if (logger.isInfoEnabled()) {
			logger.info("Root WebApplicationContext: initialization started");
		}
		long startTime = System.currentTimeMillis();

		try {
			// Store context in local instance variable, to guarantee that
			// it is available on ServletContext shutdown.
			if (this.context == null) {
				this.context = createWebApplicationContext(servletContext);
			}
			if (this.context instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
				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 ->
						// determine parent for root web application context, if any.
						ApplicationContext parent = loadParentContext(servletContext);
						cwac.setParent(parent);
					}
					configureAndRefreshWebApplicationContext(cwac, servletContext);
				}
			}
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			if (ccl == ContextLoader.class.getClassLoader()) {
				currentContext = this.context;
			}
			else if (ccl != null) {
				currentContextPerThread.put(ccl, this.context);
			}

			if (logger.isDebugEnabled()) {
				logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
						WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
			}
			if (logger.isInfoEnabled()) {
				long elapsedTime = System.currentTimeMillis() - startTime;
				logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
			}

			return this.context;
		}
		catch (RuntimeException ex) {
			logger.error("Context initialization failed", ex);
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
			throw ex;
		}
		catch (Error err) {
			logger.error("Context initialization failed", err);
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
			throw err;
		}
	}


对于SpringMVC,ContextLoaderListener对象是不是必须的呢?应该不是,它加载的是除SpringMVC之外的上下文(非Web类的)。而DispatcherServlet也会做一遍SpringMVC相关的上下文初始化,参考某位前辈们的图:


只不过,如果不配置它,就不能使用Spring的普通容器了,也就不能依赖注入那些普通beans了。另外这里可以跟Spring的IoC机制呼应上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值