zoukankan      html  css  js  c++  java
  • Purpose of ContextLoaderListener in Spring

    The ApplicationContext is where your Spring beans live. The purpose of the ContextLoaderListener is two-fold:

    1. to tie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext and

    2. to automate the creation of the ApplicationContext, so you don't have to write explicit code to do create it - it's a convenience function.

    Another convenient thing about the ContextLoaderListener is that it creates a WebApplicationContext and WebApplicationContext provides

    access to the ServletContextvia ServletContextAware beans and the getServletContext method.

    ServletContext定义了一些方法方便Servlet和Servlet容器进行通讯,在一个web应用中所有的Servlet都共用一个ServletContext,Spring在和web应用结合使用的时候,

    是将Spring的容器存到ServletContext中的;而ServletContextListener用于监听ServletContext一些事件。

     
    Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.
     
    1.分析
    public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
        /**
         * Initialize the root web application context.
         */
        @Override
        public void contextInitialized(ServletContextEvent event) {
            initWebApplicationContext(event.getServletContext());
        }
    
    
        /**
         * Close the root web application context.
         */
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            closeWebApplicationContext(event.getServletContext());
            ContextCleanupListener.cleanupAttributes(event.getServletContext());
        }

    在initWebApplicationContext里面有详细信息

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

    由最后一句可知,WeApplicationContext的实例,存放在servletContext中。

    文件内容:

    # Default WebApplicationContext implementation class for ContextLoader.
    # Used as fallback when no explicit context implementation has been specified as context-param.
    # Not meant to be customized by application developers.

    org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
     
     
  • 相关阅读:
    Pytest单元测试框架之FixTure内置临时文件tmpdir操作
    Python+Request库+第三方平台实现验证码识别示例
    Pytest单元测试框架之parametrize参数化
    Pytest单元测试框架之setup/teardown模块示例操作
    mysql匿名账户登录导致的"ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'"错误
    求全排列
    (转载)你真的会写单例模式吗——Java实现
    《大话设计模式》之--第14章 老板回来,我不知道----观察者模式
    关于java Integer大小比较的问题
    Java char[] 数组转为 String 的两种方式
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/5225170.html
Copyright © 2011-2022 走看看