使用 ContextLoaderListener 创建 ApplicationContext 时,必须使服务器支持 listener,下面这些服务器都是支持Li stener 的,如果使用这些服务器,则可以使用ContextLoaderListener创建 ApplicationContext 实例:
Apache Tomcat4.x+ 。
Jetty 4.x+。
Resin2.1.8+。
Orion 2.0.2+。
BEAWebLogic8.1SP3。
Spring 提供 SeryletContextListener 的一个实现类 ContextLoaderListener,该类可以作为 listener 使用,它会在创建时自动查找 WEB-INF/下的 applicationContext.xrnl 文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xrnl,则只需在 web.xrnl文件中增加如下代码即可:
<1istener>
<listener-class>org.springframework.web.context.ContextLoaderListener</
listener-class></listener>
如果有多个配置文件需要载入,则考虑使用<context-para即元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时参数名字应该是contextConfigLocation。
带多个配置文件的web.xrnl文件如下:
<!--XML 文件的文件头--〉
<?xml version="l.O"encoding="i80-8859-1"?>
<1-- web.xm1文件的DTD等信息--〉<!DOCTYPE web-appPUBLIC
"-//Sun Microsystems.i口c.//DTDWeb Application 2.3//
EN"''http://java.sun.com/dtd/web-app_2_3.dtd''><web-app>
<!--确定多个配置文件>
<context-param><1--参数名为 contextConfigLocation…〉
<param-name>contextConfigLocation</param-name>
<!--多个配置文件之间以,隔开--〉
<param-value>/WEB-iNF/daoContext.xml./WEB-INF/application
Context.xml</param-value></context-param><!--
采用 listener创建ApplicationContext实例-->
<listener>
<listener-class>org.springframework.web.context.ContextLoader
Listener</listener-class></listener></web-app>
如果没有 contextConfigLocation指定配置文件,则 Spring 自动查找 applicationContext.xrnl 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。该参数指定的一个字符串,Spring 的 ContextLoaderListener 负责将该字符串分解成多个配置文件,逗号","、空格""及分号";"都可作为字符串的分割符。
如果既没有applicationContext.xrnl 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring无法加载配置文件或无法正常创建 ApplicationContext 实例。
Spring 根据 bean 定义创建 WebApplicationContext 对象,并将其保存在 Web 应用的ServletContext 中。大部分情况下,应用中的bean 无须感受到 ApplicationContext的存在,只要利用 ApplicationContext的 JoC 即可。
如果需要在应用中获取ApplicationContext实例,则可以通过如下方法获取:
WebApplicationContextctx:
WebApplicationContextUtils.getWebApplicationContext(servletContext);
下面是采用 Servlet获取 ApplicationContext 的完整源代码:
public class SpringTestServlet extends HttpServlet
//Servlet的响应方法。
public voidservice(HttpServletRequestrequest,HttpServletResponse response)
throwsServletExceptio口, java.io.IOException
//获取 Servlet的 ServletContext对象
ServletContext sc = getServletContext();
//使用 WebApplicationContextUtils类获得ApplicationContext
WebApplicationContext ctx =WebApplicationContextUtils.
getWebApplicationContext(sc);
//获取 Servlet的页面输出流
PrintWriter out = response.getWriter();
//将 ApplicationContext对象输出
out.println(ctx);
可在程序里手动获取ApplicationContext对象,然后直接输出到Servlet 的响应。将看到 ApplicationContext加载了 web.xml文件中指定的两个配置文件。
分享到:
相关推荐
Spring的监听器ContextLoaderListener的作用
web.xml中ContextLoaderListener的运行
NULL 博文链接:https://angie.iteye.com/blog/2334955
java解决org.springframework.web.context.ContextLoaderListener
ssh整合时 被虐的经验之谈。内容虽然比较少,也是前人的工作经验。
spring/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> ApplicationContext * * * *...
二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...
本文介绍了Listener以下几个方面的内容: ... Spring使用ContextLoaderListener加载ApplicationContext配置信息 Spring使用Log4jConfigListener配置Log4j日志 Spring使用IntrospectorCleanupListener清理
采用Spring+Hibernate方式实现 2.Service改造工作 采用Spring的IoC注入方式使用DAO 3.Action改造工作 采用整合插件注入方式使用Spring容器中的Service或DAO. (默认按名称匹配规则) 4.配置信息的改造 Service和...
org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为...
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml ...
api的依赖在/ web-spring-tutorial / src / main / webapp / views中创建hello.jsp 在pom.xml中添加spring-webmvc依赖项修改/WEB-INF/web.xml:添加contextConfigLocation和ContextLoaderListener 创建/WEB-INF/...
/WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml <param-name>log4jConfigLocation <param-value>/WEB-INF/log4j.properties <!-- Listener contextConfigLocation --> ...
下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...
Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context.ContextLoaderListener 。 Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的...
<!-- 前端控制器:...-- 启动WEB容器时,自动装配ApplicationContext配置信息:ContextLoaderListener --> <!-- 确保web应用程序的类加载器以及加载的类正确释放资源:IntrospectorCleanupListener -->
<listener-class>org.springframework.web.context.ContextLoaderListener </listener> <param-name>contextConfigLocation <param-value>classpath:applicationContext.xml </context-param>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener> <param-name>contextConfigLocation <param-value>classpath:applicationContext.xml </context-param>
<param-value>classpath*:applicationContext-*.xml <!-- 这是spring 监听 --> <listener-class>org.springframework.web.context.ContextLoaderListener <!-- 利用spring监听 编码设置 --> ...
在web应用启动读取web.xml时,发现配置了ContextLoaderListener,而ContextLoaderListener实现了ServletCo