`
wlh269
  • 浏览: 453369 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring(十五)的监听器ContextLoaderListener的作用

阅读更多
在SSH项目中我们自动加载spring配置文件需要在web.xml文件中添加一段配置:
  <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>

===========================================
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,书上都没怎么详细说明。现在的方法就是查看它的API文档。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。看看它的API说明



第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet。 这个接口

    第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory这样一来spring中的所有bean都由这个类来创建

    第三段,讲如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext-*.xml
        </param-value>
    </context-param>

   在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入

分享到:
评论

相关推荐

    Spring的监听器ContextLoaderListener的作用

    Spring 的监听器 ContextLoaderListener 的作用 ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    【Spring在Web项目中的应用】 ...同时,通过监听器`ContextLoaderListener`,可以在Web容器启动时自动加载Spring配置,确保在整个Web应用程序生命周期中,Service层和其他Spring管理的bean都可以正确地被创建和管理。

    java解决org.springframework.web.context.ContextLoaderListener

    这个监听器是基于Servlet容器(如Tomcat、Jetty等)的,当Web应用启动时,它会读取配置文件(通常是`web.xml`),创建并加载ApplicationContext。如果在这个过程中遇到问题,比如`ClassNotFoundException`,那通常...

    精品专题(2021-2022年收藏)spring项目中监听器作用.doc

    本文将深入探讨Spring框架中的监听器,特别是`ContextLoaderListener`的作用及其配置。 `ContextLoaderListener`是Spring框架提供的一个核心监听器,主要用于初始化和销毁Spring的WebApplicationContext。它是...

    web.xml中ContextLoaderListener的运行过程解析

    `ContextLoaderListener`是Spring框架中的一个监听器,它负责初始化Spring应用上下文。下面将详细解析`web.xml`中`ContextLoaderListener`的运行过程。 ### 1. `web.xml`的作用 `web.xml`文件主要用来定义Servlet、...

    DispatcherServlet 和 ContextLoaderListener 区别

    ContextLoaderListener则是Spring容器的启动监听器,它负责初始化Spring应用上下文(ApplicationContext)。当Web应用启动时,ContextLoaderListener会读取Web-INF下的applicationContext.xml配置文件,创建并加载...

    ssh整合时遇到常见错误 ContextLoaderListener not found 解决

    这个错误通常出现在尝试启动基于Spring MVC的Web应用程序时,因为它无法找到用于初始化Spring应用上下文的监听器。 `ContextLoaderListener`是Spring框架的一部分,它负责在Web应用启动时加载并管理Spring的应用上...

    监听器获取Spring配置文件的方法

    这种方式虽然简单,但存在一个问题:当Spring的`ContextLoaderListener`已经加载了配置文件后,我们的监听器再次加载会导致bean被实例化两次,增加了不必要的资源消耗。 2. **从ServletContext中获取** 既然...

    监听器Listener

    这是一个重要的Spring监听器,负责初始化Spring的Web应用上下文。通过在web.xml中配置,它可以确保在Web应用启动时加载Spring的根应用上下文。 5. **CharacterEncodingFilter**: 虽然不是一个监听器,但通常与...

    Spring-5.1.5源码

    3. **监听器注册**:在web.xml中,我们需要将`ContextLoaderListener`注册为一个Servlet监听器,以便在Web应用启动时调用: ```xml &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ...

    Spring笔记.doc

    监听器如 ContextLoaderListener 在项目启动时加载 Spring 配置文件并保存到 application 对象中,WebApplicationContextUtils 可以从 application 对象中获取 Spring 上下文。 【Spring 与 Hibernate 整合】 ...

    struts加载spring的方法

    2. **配置Spring监听器**:在`web.xml`文件中,需要配置一个Spring的上下文监听器`ContextLoaderListener`,该监听器负责初始化Spring的ApplicationContext。具体配置如下所示: ```xml &lt;listener-class&gt;org....

    spring 与 servlet整合

    2. **Spring的Servlet监听器**:如ContextLoaderListener,用于初始化Spring的ApplicationContext,加载配置文件并管理bean。 3. **HandlerMapping**:负责将请求映射到相应的处理器,Spring MVC提供了多种映射策略...

    加载spring 文件,在web.xml中的配置

    `&lt;listener&gt;`标签中的`&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;`定义了一个监听器,它会在Web应用启动时自动加载默认的Spring配置文件,即`/WEB-INF/...

    spring和xfire配置

    接下来,配置Spring的监听器`ContextLoaderListener`。这个监听器在Web应用程序启动时会加载Spring配置文件,以便初始化Spring容器。在这里,我们指定了两个配置文件的位置:一个是XFire自身的配置文件`xfire.xml`,...

    Spring mvc + Spring + Spring jdbc 整合 demo.rar

    - 配置Web.xml文件,设置DispatcherServlet的初始化参数和监听器,以便加载Spring上下文。 - 编写Controller,处理HTTP请求,并调用Service完成业务逻辑。 5. **项目源码分析**:在压缩包中的项目源码,我们可以...

    spring配置步骤

    这里的`ContextLoaderListener`监听器会在Web应用启动时加载Spring的应用上下文,从而确保Spring能够在服务器启动时正确初始化。 ##### 3. 创建`applicationContext.xml`文件 在`WEB-INF`目录下创建一个名为`...

    Spring定时器配置详解

    `ContextLoaderListener`监听器会在Web应用启动时加载配置文件,并创建Spring的ApplicationContext,从而激活定时任务。 总结来说,Spring定时器的配置主要包括创建任务类、在配置文件中定义任务和调度规则,以及在...

    Spring与Web环境集成.pdf

    Spring框架提供了一个监听器ContextLoaderListener,就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中。同时,Spring还提供了一个客户端工具...

    spring中的所有配置

    在Spring框架中,`ContextLoaderListener`是一个非常重要的监听器,它用于初始化Web应用程序上下文。当Web容器启动时,它会触发`ContextLoaderListener`,该监听器将读取并创建一个WebApplicationContext。这通常在`...

Global site tag (gtag.js) - Google Analytics