Spring ContextLoaderListener 中WebApplicationContext初始化

來源:互聯網
上載者:User

標籤:spring   contextloaderlistener   webapplicationcontext初始化   

    在spring和structs2整合中,在web.xml檔案中需要設定一個listener,如下

    

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

    ContextLoaderListener會在web容器初始化的時候進行初始化,然後進行初始化WebApplicationContext的工作。我們來看一下ContextLoaderListener的關鍵代碼:

    

    public void contextInitialized(ServletContextEvent event) {        this.contextLoader = createContextLoader();        if (this.contextLoader == null) {            this.contextLoader = this;        }        this.contextLoader.initWebApplicationContext(event.getServletContext());    }

        這段代碼實現了WebApplicationContext初始化工作,在代碼的最後一行調用了contextLoader的initWebApplicationContext的方法,我們再來看一下contextLoader的initWebApplicationContext方法的關鍵代碼:

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

最後一行,又調用了createWebApplicationContext方法,我們再來看一下這個方法的代碼:

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

通過代碼可知,在這裡返回了一個ConfigurableWebApplicationContext,再來看一下contextLoader的initWebApplicationContext方法中最關鍵的代碼:

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

在這裡把context存入servletContext中,所以以後要用到WebApplicationContext的時候可以從servletContext取出。

本文出自 “飛魚技術” 部落格,請務必保留此出處http://flyingfish.blog.51cto.com/9580339/1681334

Spring ContextLoaderListener 中WebApplicationContext初始化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.