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;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章