springmvc学习3 ContextLoaderListener 初始化webApplicationContext

在web.xml 中会配置一个listener监听器 这个监听器是来初始化webApplicationContext 

当tomcat启动的时候会首先进入到这个监听器中 contextInitialized方法 调用了父类ContextLoader 中的initWebApplicationContext 方法

看看这个方法都干了什么

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

//首先进来先获取了一个servletContext的参数 暂时不知道这个参数的作用 tomacat 启动的时候 为null 所以直接跳过这个判断

        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) {

//这里实例化一个webApplicationContext 

//  在实例化的时候 首先会现从web.xml 中读取有没有配置contextClass 的参数

//如果没有的话 会从spring启动时候加载的配置文件中获取 WebApplicationContext.class.getName()

//再通过反射 ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader())  返回一个class对象

// 最后通过 BeanUtils.instantiateClass(contextClass) 实例化这个webApplicationContext   其实是 ConfigurableWebApplicationContext这个类 是webApplicationContext的

//子类

                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.

//这里应该是加载了beanFactory  在这次init的时候基本什么也没做就返回了parent

                        ApplicationContext parent = loadParentContext(servletContext);
                        cwac.setParent(parent);

                    }

//这里获取了web.xml 中servletcontextPath 作为webApplicationContext 的id  和 获取ConfigLocation 给webApplicationContext 中set进去

                    configureAndRefreshWebApplicationContext(cwac, servletContext);
                }

            }

//设置了一个值

            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
//这个classLoader还没看明白
            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;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值