`
mxl86
  • 浏览: 174829 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring中的ContextLoaderListener作用

阅读更多

总得来说,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的作用

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

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

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

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

    在Java Web开发中,`org.springframework.web.context.ContextLoaderListener` 是Spring框架的一部分,它负责初始化一个Web应用程序的Spring上下文。这个监听器是基于Servlet容器(如Tomcat、Jetty等)的,当Web应用...

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

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

    DispatcherServlet 和 ContextLoaderListener 区别

    在Java Web开发中,DispatcherServlet和ContextLoaderListener都是Spring框架中的关键组件,它们各自承担着不同的职责,共同构建了一个高效、灵活的Web应用程序。这里我们将深入探讨这两个组件的区别及其工作原理。 ...

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

    在IT行业中,SSH整合指的是Spring、Struts和Hibernate这三个开源框架的集成,它们共同构建了一个强大的企业级Web应用开发解决方案。然而,在实际操作过程中,开发者经常会遇到各种问题,其中之一就是...

    spring的bean作用域

    为了使用Request作用域,需要在web.xml中配置ContextLoaderListener或DispatcherServlet。 - 示例配置:`&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/&gt;` 4. **Session作用域**: - ...

    Spring4整合Jersey2.9

    3. **配置Jersey**:在`web.xml`中设置Jersey的Servlet,同时配置Spring的ContextLoaderListener,以加载Spring配置: ```xml &lt;servlet-name&gt;jersey-serlvet &lt;servlet-class&gt;org.glassfish.jersey.servlet....

    Spring中ApplicationContext加载机制

    Spring中ApplicationContext加载机制 ApplicationContext 是 Spring 框架中的核心组件之一,负责加载和管理应用程序中的 Bean 对象。在 Web 应用程序中,ApplicationContext 的加载机制是非常重要的, Spring 提供...

    spring springmvc mybatis框架整合需要的jar包

    最后,应用的启动入口通常是一个Servlet或Filter,例如Spring的ContextLoaderListener或DelegatingFilterProxy,它们负责初始化Spring容器,并在Web应用启动时加载配置。 总结起来,整合Spring、SpringMVC和MyBatis...

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

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

    spring MVC配置详解

    在 web.xml 文件中配置 ContextLoaderListener 和 contextConfigLocation,以便加载 Spring 的配置文件。 ```xml &lt;!-- Spring 配置 --&gt; org.springframework.web.context.ContextLoaderListener ...

    struts+spring

    6. **过滤器配置**:在Web应用的部署描述符`web.xml`中,需要配置Struts的Filter和Spring的ContextLoaderListener。Struts Filter负责处理HTTP请求,而ContextLoaderListener则初始化Spring的ApplicationContext。 ...

    整合spring+dorado

    &lt;listener-class&gt;com.bstek.dorado.context.spring.SpringContextLoaderListener&lt;/listener-class&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:spring-context.xml ... ``` 在Dorado的...

    Spring-5.1.5源码

    在这个版本中,我们主要关注`spring-web`模块中的`org.springframework.web.context.ContextLoaderListener`类,它是Spring MVC应用的核心部分。 `ContextLoaderListener`是一个实现了`javax.servlet....

    maven与spring MVC

    【标题】"maven与spring MVC"涉及到的是Java Web开发中的两个关键组件——Maven和Spring MVC,它们在构建和管理复杂项目时起着至关重要的作用。Maven是一个项目管理和综合工具,它通过提供一个标准化的构建过程和...

    Spring定时器配置详解

    Spring定时器,也被称为Spring的定时任务管理,是Spring框架中的一个强大功能,允许开发者在应用程序中安排周期性的任务执行。这个特性主要依赖于Quartz库,一个流行的Java作业调度框架,但Spring提供了更简单的API...

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....

    spring中的所有配置

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

    Spring笔记.doc

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

Global site tag (gtag.js) - Google Analytics