当前位置:文档之家› web.xml配置解析

web.xml配置解析

一.监听器:1.ContextLoaderListener配置信息:<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参数:view plaincopy to clipboardprint?<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的官方给出的档案,写成:view plaincopy to clipboardprint?<!-- Context Configuration locations for Spring XML files --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml </param-value></context-param>2.Log4jConfigListener配置信息:1.<context-param>2.<param-name>webAppRootKey</param-name>3.<param-value>xx.root</param-value>4.</context-param>5.6.<context-param>7.<param-name>log4jConfigLocation</param-name>8.<param-value>/WEB-INF/classes/log4j.xml</param-value>9.</context-param>10.11.<context-param>12. <param-name>log4jRefreshInterval</param-name>13. <param-value>60000</param-value>14.</context-param>15.16.<listener>17. <listener-class>org.springframework.web.util.Log4jConfigListener18. </listener-class>19.</listener>配置解释:使用spring中的Log4jConfigListener有如如下好处:1. 动态的改变记录级别和策略,不需要重启Web应用2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。

因为系统把web目录的路径压入一个叫webapp.root的系统变量。

这样写log文件路径时不用写绝对路径了.log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。

4.log4jRefreshInterval为60000,表示开一条watchdog线程每60秒扫描一下配置文件的变化.5.<context-param><param-name>webAppRootKey</param-name><param-value>web.sample.root</param-value></context-param>* 可以用System.getProperty("web.sample.root")来获取属性值。

在Eclipse调试Web 项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过上述语句打印出属性值,来看看临时项目路径在哪里。

* Spring通过org.springframework.web.util.WebAppRootListener这个监听器来压入项目路径。

但是如果在web.xml中已经配置了org.springframework.web.util.Log4jConfigListener这个监听器,则不需要配置WebAppRootListener了。

* 因为Log4jConfigListener已经包含了WebAppRootListener的功能部署在同一容器中的Web项目,要配置不同的<param-value>,不能重复。

* 如果配置了log4j.appender.file.File=${web.sample.root}/WEB-INF/logs/sample.loglog4j会自己自动建立logs目录, 不需要手工显式建立空的logs目录。

6.log4j.xml配置示例:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='/log4j/'> <!-- 将日志信息输出到控制台--><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/></layout></appender><!-- 以下根据级别将日志信息输出到不同的文件中--><!--设定级别为debug的配置信息--><appender name="DEBUG" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="C:\\logs\\app-debug.log"/><param name="Append" value="true"/><param name="MaxFileSize" value="500MB"/><param name="MaxBackupIndex" value="2"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="DEBUG" /><param name="LevelMax" value="DEBUG" /></filter></appender><!--设定级别为info的配置信息--><appender name="INFO" class="org.apache.log4j.RollingFileAppender"><param name="File" value="C:\\logs\\app-info.log"/><param name="Append" value="true"/><param name="MaxFileSize" value="500KB"/><param name="MaxBackupIndex" value="2"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="INFO" /><param name="LevelMax" value="INFO" /></filter></appender><!--设定级别为warn的配置信息--><appender name="WARN" class="org.apache.log4j.RollingFileAppender"><param name="File" value="C:\\logs\\app-warn.log"/><param name="Append" value="true"/><param name="MaxFileSize" value="500KB"/><param name="MaxBackupIndex" value="2"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="WARN" /><param name="LevelMax" value="WARN" /></filter></appender><!--设定级别为deb的配置信息--><appender name="ERROR" class="org.apache.log4j.RollingFileAppender"><param name="File" value="d:\\logs\\error.log"/><param name="Append" value="true"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="ERROR" /><param name="LevelMax" value="ERROR" /></filter></appender><!--配置具体要实现的方式,这边只设定了STDOUT,ERROR这2种情况,没有使用到的情况,不要在上面配置,否则会出错--><root><priority value="info"/><appender-ref ref="STDOUT"/><appender-ref ref="ERROR"/></root></log4j:configuration>3.IntrospectorCleanupListener配置信息:<listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener </listener-class></listener>配置解释:org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露,它是一个在web应用关闭时清除JavaBean Introspector的监听器,在web.xml中注册这个listener可以保证在web应用关闭的时候释放掉与这个web应用相关的class loader和由它管理的类.4.RequestContextListener配置信息:<listener><listener-class>org.springframework.web.context.request.RequestContextListener </listener-class></listener>配置解释:在整合spring容器时使用ContextLoaderListener,它实现了ServletContextListener监听器接口,ServletContextListener只负责监听web容器启动和关闭的事件.而RequestContextListener实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,web服务器接收的每一次请求都会通知该监听器.spring容器启动和关闭操作由web容器的启动和关闭事件触发,但如果spring容器中的Bean需要request,session,globalsession作用域的支持,spring容器本身就必须获得web容器的HTTP请求事件,以HTTP请求的事件"驱动"Bean作用域的控制逻辑.二.Filter(过滤器)1.CharacterEncodingFilter配置信息:<filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>配置解释:使用Spring中的过滤器解决在请求和应答中的中文乱码问题。

相关主题