首先这些接口全部来自于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
//设置WebApplicationContext的id,保存WebApplicationContext到servletcontext中
-->configureAndRefreshWebApplicationContext—>customizeContext一般是空—>determineContextInitializerClasses一般是空
AbstractApplicationContext—>refresh开启了spring的大门
由上可以看出XmlWebApplicationContext-->AbstractApplicationContext—>refresh()开启了spring的大门