web启动spring过程

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

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

Spring 在web环境中通过ContextLoaderListener监听器启动,通过加载指定目录下的spring配置文件,Web服务器启动spring的初始化过程,如果没有指定,spring会默认加载
/WEB-INF 下的applicationContext.xml.
ContextLoaderListener实现了ServletContextListener接口,当ContextLoaderListener监听器启动时调用contextInitialized方法初始化spring的web应用上下文。

/**
     * Initialize the root web application context.
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
        //初始化web应用上下文
        initWebApplicationContext(event.getServletContext());
    }

contextLoaderListerner初始化web应用上下文源码如下:

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
        判断在Servlet上下文中Spring Web应用给根上下文是否已经存在 
        if (servletContext.getAttribute(WebApplicationContext.
                ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
            throw new IllegalStateException(
                    "Cannot initialize context because there is already a root " +
                    "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()) { //是否是info级别日志, 提升性能
            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); //创建web应用上下文
            }
            if (this.context instanceof ConfigurableWebApplicationContext) {
            ConfigurableWebApplicationContext cwa=(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();
            //如果当前线程的类加载器是contextLoader,则将当前上下文设置为创建的web应用上下文
            if (ccl == ContextLoader.class.getClassLoader()) {
                currentContext = this.context;
            }
            //如果当前线程的容器类加载器不为null,则将创建的web应用上下文和其类 
            //加载器缓存在容器类加载器—>Web应用上下文Map集合中
            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);
            .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;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值