Web容器启动时加载Spring

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 04:35 点击:

 

在应用程序web.xml中做了以下配置信息时,当启动Web容器时就会自动加载Spring容器。

 

<listener> 

 

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

 

</listener> 

ContextLoaderListener类实现了javax.servlet.ServletContextListener接口并且继承了org.springframework.web.context.ContextLoader类。ServletContextListener事件类是Web容器的一部分,处理Web应用的Servlet上下文(context)的监听。实现ServletContextListener接口中的contextInitialized和contextDestroyed方法。当Web容器启动时会自动调用contextInitialized方法,进行初始化Spring Web应用程序上下文,主要加载web.xml中contextConfigLocation的配置文件;当Web容器关闭之前会调用contextDestroyed方法,进行销毁Spring Web应用程序上下文。ContextLoader类实现了Spring上下文初始化的工作,执行initWebApplicationContext方法返回WebApplicationContext。Spring实现的contextInitialized和contextDestroyed代码如下:

 

public void contextInitialized(ServletContextEvent event) { 

 

       this.contextLoader = createContextLoader(); 

 

       if (this.contextLoader ==null) { 

 

           this.contextLoader = this; 

 

       } 

 

    this.contextLoader.initWebApplicationContext(event.getServletContext()); 

 

    } 

 

  

 

public void contextDestroyed(ServletContextEvent event) { 

 

       if (this.contextLoader !=null) {       

 

this.contextLoader.closeWebApplicationContext(event.getServletContext()); 

 

 

 ContextCleanupListener.cleanupAttributes(event.getServletContext()); 

 

  

Spring执行实现Spring上下文的方法

 

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

&nb

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Gb2312转utf-8编码的方法(vbs+js)
      • 如何使用Ajax技术开发Web应用程序(1)
      • js跳转路径问题
      • JavaScript模仿桌面窗口
      • 用js检测两个线段是否相交
      • 运用JavaScript构建你的第一个Metro式应用程序(on Windows
      • 我知道的JavaScript -- 设计模式(桥接)应用之 – 验证器
      • 我是如何去了解jquery的(六),案例之幻灯片轮换
      • Jquery封装幻灯片效果
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1