每日一则JavaWeb---Spring之ContextLoaderListener

本文详细解析了Spring Web启动过程中的关键组件ContextLoaderListener的工作原理,包括如何通过web.xml配置初始化上下文,以及如何利用ServletContextListener接口触发上下文的创建与销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先这些接口全部来自于4.1版本的spring-web.jar包

package org.springframework.web.context;

先看一下ContextLoaderListener的继承结构

public class ContextLoaderListener extends ContextLoader implements ServletContextListener

可以看到其中涉及了ContextLoader、ServletContextListener。

首先看看ServletContextListener的源码,很简单:

public interface ServletContextListener extends EventListener {
 
    public void contextInitialized(ServletContextEvent sce);

    public void contextDestroyed(ServletContextEvent sce);
}

也就是说容器初始化和容器关闭的时候会做一些事情,其中的参数ServletContextEvent也比较简单,就是将ServletContext序列化而且包了一层

public class ServletContextEvent extends java.util.EventObject { 

	/** Construct a ServletContextEvent from the given context.
	 *
	 * @param source - the ServletContext that is sending the event.
	 */
    public ServletContextEvent(ServletContext source) {
	super(source);
    }
    
	/**
	 * Return the ServletContext that changed.
	 *
	 * @return the ServletContext that sent the event.
	 */
    public ServletContext getServletContext () { 
	return (ServletContext) super.getSource();
    }
    
}
其中EventObject是就是把Object包了一层序列化的东西,当然哈重写了toString

public class EventObject implements java.io.Serializable {

    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object  source;

    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
        return source;
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }

下面再看看,ContextLoader有什么幺蛾子。。。具体参见:http://blog.youkuaiyun.com/chenpeng19910926/article/details/71195202

解释上说的话:当ContextLoaderListener 被调用的时候当 root application context真正初始化的时候被调用

做了啥事情:

1. 查找web.xml中

  <context-param>
        <param-name>contextClass</param-name>
        <param-value>com.chenpeng.test</param-value>
    </context-param>
来指定context class的类型:默认是
org.springframework.web.context.support.XmlWebApplicationContext

 所有指定的context class 都需要实现接口

ConfigurableWebApplicationContext

2.处理web.xml中的contextConfigLocation

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring4-context.xml</param-value>
    </context-param>

看看上面ContextLoader在看看ContextLoaderListener的代码就清晰了很多

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

	public ContextLoaderListener() {
	}

	public ContextLoaderListener(WebApplicationContext context) {
		super(context);
	}
	//Initialize the root web application context.来自于ServletContextListener
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());//ContextLoader
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		closeWebApplicationContext(event.getServletContext());//ContextLoader
		ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}

}

initWebApplicationContext

     -->createWebApplicationContext(servletContext)—>XmlWebApplicationContext

//设置WebApplicationContextid,保存WebApplicationContextservletcontext

      -->configureAndRefreshWebApplicationContext—>customizeContext一般是空—>determineContextInitializerClasses一般是空

                                                                                      AbstractApplicationContext—>refresh开启了spring的大门

由上可以看出XmlWebApplicationContext-->AbstractApplicationContext—>refresh()开启了spring的大门


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值