`
liyuanchao2004
  • 浏览: 8054 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

ContextLoaderListener 详解

    博客分类:
  • web
阅读更多
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
作用就是启动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等文件,都会一同被载入。

由此可见applicationContext.xml的文件位置就可以有两种默认实现:

第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener;

第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>,用它来指明你的applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
至于整合spring mvc 过程中会有如下情况
创建一个全局的WebApplicationContext上下文,叫做root上下文,我们一般会将事物管理bean、任务调度bean放在这个上下文中,以便子上下文继承。
org.springframework.web.servlet.DispatcherServlet该类是一个servlet,web应用中可以创建多个servlet,每个servlet都有自己的WebApplicationContext ,同时还继承了上面提到的root上下文,能够使用root上下文中的所有bean。
在springnvc中既可以使用父子上下文(配置ContextLoaderListener),也可以不使用root上下文。
分享到:
评论

相关推荐

    SpringMVC框架搭建及详解.pdf

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ``` * 配置context-param: ```xml &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:config/applicationContext.xml ``` ...

    详解Spring mvc的web.xml配置说明

    在Spring MVC中,`org.springframework.web.context.ContextLoaderListener`是一个关键监听器,它实现了`ServletContextListener`接口。当Web应用程序启动时,Tomcat或Jetty等容器会触发此监听器,进而加载Spring的...

    Spring MVC 框架搭建及详解

    3. **ContextLoaderListener和applicationContext.xml**: `&lt;listener&gt;`元素中的`ContextLoaderListener`监听器负责初始化Spring的ApplicationContext,读取`applicationContext.xml`配置文件,该文件通常包含应用...

    spring MVC配置详解

    Spring MVC 配置详解 Spring MVC 是一个基于 DispatcherServlet 的 MVC 框架,它是当前主流的 Web 框架之一。要想灵活运用 Spring MVC 来应对大多数的 Web 开发,就必须要掌握它的配置及原理。 一、Spring MVC ...

    Flex与Spring整合详解

    【Flex与Spring整合详解】 Flex,全称Adobe Flex,是一种用于构建富互联网应用程序(RIA)的开源框架,它允许开发者创建交互性强、图形丰富的Web应用。而Spring,则是Java领域中广泛使用的轻量级服务器端框架,提供...

    Spring定时器配置详解

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

    Struts配置文件使用及代码详解

    3. **Web.xml配置**:配置Struts2的监听器和过滤器,比如ContextLoaderListener和StrutsPrepareAndExecuteFilter,确保所有请求都能被Struts2拦截并处理。 以上便是Struts2配置文件的使用和代码详解,通过理解这一...

    SpringMVC配置详解.pdf

    除了`DispatcherServlet`配置,还需要设置`ContextLoaderListener`监听器,它会在应用启动时加载Spring的全局配置文件,通常是`applicationContext.xml`。`context-param`用于指定该配置文件的位置,例如`classpath:...

    springMVC框架搭建及详解

    ### SpringMVC框架搭建及详解 #### 一、SpringMVC环境搭建 SpringMVC作为当前Web开发领域中与Struts并驾齐驱的主流框架之一,其灵活度和适应性使其成为众多开发者掌握的核心技能。为了能够熟练地运用SpringMVC解决...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    Spring 获取 WebApplicationContext、ApplicationContext 几种方法详解 在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的...

    SSM基本的配置详解

    ### SSM基本配置详解 #### 一、简介 在Java Web开发领域中,Spring MVC、MyBatis 和 Spring(简称 SSM)框架组合是构建基于Java的企业级应用系统的常用架构之一。SSM框架通过整合Spring MVC作为前端控制器,...

    J2EE的SSH配置详解

    - **listener**:配置监听器,如Spring的ContextLoaderListener,用于初始化Spring应用上下文。 ```xml &lt;filter-name&gt;struts2 &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter....

    SSH整合的配置文件详解

    例如,在SSH框架整合中,以下代码段展示了如何配置Spring的`ContextLoaderListener`,以便在启动时加载Spring的配置文件: ```xml org.springframework.web.context.ContextLoaderListener &lt;param-name&gt;...

    MyEclipse整合Struts2+Spring+Hibernate详解

    ### MyEclipse整合Struts2+Spring+Hibernate详解 #### 一、概述 本文档将详细介绍如何使用MyEclipse集成开发环境(IDE)搭建并整合Struts2、Spring与Hibernate这三个流行的企业级Java开发框架。通过本教程的学习,...

    SpringMVC配置详解[借鉴].pdf

    `ContextLoaderListener`读取`contextConfigLocation`参数指定的`applicationContext.xml`配置文件,通常存放于`classpath:config/`目录下。这个配置文件用于定义应用范围内的Bean。 然后,我们来看`spring-servlet...

    SSH框架整合步骤详解

    - 在`web.xml`中配置Spring的`ContextLoaderListener`,加载Spring的上下文。 - 创建Spring的配置文件(如`applicationContext.xml`),定义Bean的定义,包括DAO、Service、Action等组件。 - 使用Spring的AOP进行...

Global site tag (gtag.js) - Google Analytics