ContextLoaderListener(1)---WebApplicationContext创建过程

来源:互联网 发布:mac微信语音导出软件 编辑:程序博客网 时间:2024/05/01 12:53

ContextLoaderListener


  • ContextLoaderListener
    • Servlet容器实例化ContextLoaderListener
    • contextInitialized方法内部实现
  • ContextLoader
    • 初始化ApplicationContext initWebApplicationContext
    • 创建ApplicationContext createWebApplicationContext
    • 决定使用哪个WebApplicationContextdetermineContextClass
  • 总结
    • 配置webxml
    • 彻底晕了后面的还是画个图吧

Servlet容器实例化ContextLoaderListener

Servlet容器会实例化一个ContextLoaderListener

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

该类继承ContextLoader,实现了ServletContextListener接口,使之具有listener功能

public class ContextLoaderListener extends ContextLoader implements ServletContextListener

ContextLoaderListener 实现了ServletContextListener接口

public interface ServletContextListener extends EventListener {    /**     * web应用初始化开始的时候,收到通知, 所有的ServletContextListeners都会     * 收到通知,在任何filter或者servlet之前     */    public void contextInitialized(ServletContextEvent sce);    /**     * ServletContext准备关闭的时候,会调用该方法;     * 在contextDestroyed调用前,所有的servlet和filter都已经销毁了     */    public void contextDestroyed(ServletContextEvent sce);}
/**     * Initialize the root web application context.     */    public void contextInitialized(ServletContextEvent event) {        this.contextLoader = createContextLoader();        if (this.contextLoader == null) {            this.contextLoader = this;        }        this.contextLoader.initWebApplicationContext(event.getServletContext());    }    ...    /**     * Close the root web application context.     */    public void contextDestroyed(ServletContextEvent event) {        if (this.contextLoader != null) {            this.contextLoader.closeWebApplicationContext(event.getServletContext());        }        ContextCleanupListener.cleanupAttributes(event.getServletContext());    }
  1. ServletContextListener扩展了ContextLoader,使之有Listener功能
  2. public void contextInitialized(ServletContextEvent sce);方法中,获取ServletContext
  3. public void contextDestroyed(ServletContextEvent event);方法中,释放资源。

流程图描述该过程

Created with Raphaël 2.1.0Servlet容器加载实例化ContextLoaderListener调用contextInitialized(ServletContextEvent sce)End

contextInitialized方法内部实现

    /**     * Initialize the root web application context.     */    public void contextInitialized(ServletContextEvent event) {    //createContextLoader方法已经弃用,返回永远是null        this.contextLoader = createContextLoader();        if (this.contextLoader == null) {            this.contextLoader = this;        }        //调用父类initWebApplicationContext方法,传入ServletContext        this.contextLoader.initWebApplicationContext(event.getServletContext());    }    /**     * Create the ContextLoader to use. Can be overridden in subclasses.     * @return the new ContextLoader     * @deprecated in favor of simply subclassing ContextLoaderListener itself     * (which extends ContextLoader, as of Spring 3.0)     */    @Deprecated    protected ContextLoader createContextLoader() {        return null;    }

关键代码:
this.contextLoader.initWebApplicationContext(event.getServletContext());


得到信息:
1. 主要初始化任务在initWebApplicationContext中实现.


ContextLoader

1. 初始化ApplicationContext — initWebApplicationContext

关键代码:
1. this.context = createWebApplicationContext(servletContext);
2. servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
3. configureAndRefreshWebApplicationContext(cwac, servletContext);


得到信息:
1. 该代码创建了WebApplicationContext
2. 这个源代码很长(- -)
3. 关键代码在createWebApplicationContext(servletContext);里面
4. configureAndRefreshWebApplicationContext(cwac, servletContext);创建之后的配置工作,这个也很重要,以后再说,这里直说创建Context过程。


代码

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

2. 创建ApplicationContext — createWebApplicationContext

关键代码:
1. Class<?> contextClass = determineContextClass(sc);
2. return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);


得到信息:
1. 根据传入的sevletContext,返回使用WebApplicationContext接口的哪一个实现类,默认是XmlWebApplicationContext
2. 使用BeanUtils实例化该类


源代码

protected WebApplicationContext createWebApplicationContext(ServletContext sc) {        Class<?> contextClass = determineContextClass(sc);        if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {            throw new ApplicationContextException("Custom context class [" + contextClass.getName() +                    "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");        }        return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);    }

3. 决定使用哪个WebApplicationContext?—determineContextClass

关键代码:
1. String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
2. public static final String CONTEXT_CLASS_PARAM = "contextClass";
3. return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());


得到信息:
1. 从Servlet InitParam中获取的contextClassName,决定到底使用WebApplicationContext接口的哪一个实现类。
2. 如果你的web.xml中定义了如下片段,会使用你自己的WebApplicationContext (默认”contextClass”),否则就使用XmlWebApplicationContext
3. 利用反射得到Class

web.xml

<context-param>    <param-name>contextClass</param-name>    <param-value>Your ContextClass</param-value></context-param>

代码

protected Class<?> determineContextClass(ServletContext servletContext) {        String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);        if (contextClassName != null) {            try {                return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());            }            catch (ClassNotFoundException ex) {                throw new ApplicationContextException(                        "Failed to load custom context class [" + contextClassName + "]", ex);            }        }        else {            contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());            try {                return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());            }            catch (ClassNotFoundException ex) {                throw new ApplicationContextException(                        "Failed to load default context class [" + contextClassName + "]", ex);            }        }    }

总结

配置web.xml

<listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
  1. Servlet容器实例化ContextLoaderListener,并通知其调用initWebApplicationContext
  2. ContextLoaderListener调用createWebApplicationContext根据ServletContext创建出WebApplicationContext
  3. 调用configureAndRefreshWebApplicationContext进行接下来的操作

彻底晕了,后面的还是画个图吧

这里写图片描述

0 0
原创粉丝点击