log4j和web.xml配置webAppRootKey 的问题

1 在web.xml配置

<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>web.sample.root</param-value>
</context-param>

可以用System.getProperty("web.sample.root")来获取属性值。在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过上述语句打印出属性值,来看看临时项目路径在哪里

如:System.out.println("web.root:"+ System.getProperty("web.root"));

输出:web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\

2、Spring通过 org.springframework.web.util.WebAppRootListener 这个监听器来压入项目路径。但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener 这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能.

配置WebAppRootListener:

<listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

3、部署在同一容器中的多个Web项目,要配置不同的<param-value>,不能重复webAppRootKey的系统变量名

4.WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否则ApplicationContext的bean注入根目录值时会发生无法注入异常。

<!-- 项目根目录Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>
 <!--Spring的ApplicationContext 载入 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

4、如果配置了
log4j.appender.file.File=${web.sample.root}WEB-INF/logs/sample.log

log4j会自己自动建立logs目录, 不需要手工显式建立空的logs目录

在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下:

<!-- 应用路径 --> 
<context-param> 
<param-name>webAppRootKey</param-name> 
<param-value>webapp.root</param-value> 
</context-param> 
最好报纸每个项目的参数值不同,以免引起项目冲突

严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

对多个项目要对webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下,如我配置这两个项目的webAppRootKey为
项目1 的 web.xml:

<!-- 应用路径 -->
<context-param> 
<param-name>webAppRootKey</param-name> 
<param-value>webapp.root1</param-value> 
</context-param>

<!-- 项目根目录Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

项目2的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>

<!-- 项目根目录Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

这样就不会出现冲突了。
定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。
然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。

namemax:

<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>web.root</param-value>
 </context-param>

<!-- 项目根目录Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

bean中可以使用:

<bean id="transformChinese" class="com.zunmi.util.TransformChinese"

p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
         p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties" 
         p:charSet="gbk"
        />

Spring MVC中 log4j日志文件配置相对路径的更多相关文章

  1. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  2. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  3. Spring、Spring MVC、MyBatis整合文件配置详解

    原文  http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...

  4. 【转】Spring、Spring MVC、MyBatis整合文件配置详解

    见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...

  5. Spring、Spring MVC、MyBatis整合文件配置详解2

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  6. Spring、Spring MVC、MyBatis 整合文件配置详解

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  7. Spring MVC 中使用properties文件

    首先要搭建Spring mvc的环境,然后开始properties文件里的配置: 第一步:在springcontext中注入properties,具体路径自己调整 <bean id=" ...

  8. log4j日志文件配置

    ### set log levels ### ### debug (打印代码中logger.debug(xx)的信息)> info(打印代码中logger.info(xx)的信息) log4j. ...

  9. spring 配置文件中使用properties文件 配置

    配置Bean载入properties文件: <bean id="propertyPlaceholderConfigurer" class="org.springfr ...

随机推荐

  1. 李开复:AlphaGo 若打败了世界冠军,意味着什么?

    创新工场董事长李开复在知乎就AlphaGo与李世石的人机大战发表了自己看法,他认为四个月前的AlphaGo击败李世石基本不可能,不过这四个月AlphaGo进步很多,比赛应该很精彩.但是,无论这次结果如 ...

  2. Makeflie自动生成依赖,自动化编译

    在netbeans里开发,有一个重要文件makefile,是用来编译所有的文件. 项目的目录结构如下,扁平的目录结构,如何实现自动化编译,写makefile呢? 第一版 基础版: CC = g++ C ...

  3. 图形学基础 | 实现OBJ文件的载入

    1. tiny_obj_loader.h 的使用include这个头文件需要先定义一个宏 #define TINYOBJLOADER_IMPLEMENTATION#include "tiny ...

  4. this.$emit('on-select-change' emit里面不能写大写字母

    this.$emit('on-select-change' emit里面不能写大写字母 刚试了下 也能写大写 但是 两边就都写一样就完了,就都写成带-的就完了

  5. Python基础4 迭代器,生成器,装饰器,Json和pickle 数据序列化

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  6. Spring-02 Java配置实现IOC

    Java配置 Spring4推荐使用java配置实现IOC Spring boot也推荐采用java配置实现IOC 在实际项目中,一般采用注解配置业务bean,全局配置使用Java配置. Java配置 ...

  7. C#中byte类型运算

    首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...

  8. [POJ] 3539 Elevator

    http://poj.org/problem?id=3539 给定一个电梯,可以上升a,b,c层和回到1层,给定楼高h,求可达层数 lyd讲的同余类BFS,方法是先把三个量压成两个,即把h%a,因为对 ...

  9. docker-compose文件语法解析(v3.x)

    文件配置 compose文件是一个定义服务(service).网络(network)和卷(volume)的YAML文件 .Compose 文件的默认路径是 ./docker-compose.yml 提 ...

  10. 【JDBC】Servlet实例

    import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.Dri ...