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

本文详细介绍了ContextLoaderListener的工作原理,包括Servlet容器如何实例化ContextLoaderListener,以及Listener如何创建和初始化WebApplicationContext。在contextInitialized方法中,主要任务在initWebApplicationContext中执行,创建了XmlWebApplicationContext。通过determineContextClass方法,根据web.xml配置确定具体的WebApplicationContext实现。最后,总结了web.xml的配置关键,并指出后续流程的复杂性。

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

ContextLoaderListener


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.0 Servlet容器 加载实例化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进行接下来的操作

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

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值