Tomcat发布多个项目时抛的webAppRootKey错误

    最近在抽取web项目框架的时候,直接copy一份工程来做测试,当分别在tomcat下运行时,都可以正常启动,但是把2个工程同时放到tomcat启动的时间,tomcat启动就抛webAppRootKey异常。

严重: 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:\App\Tomcat\apache-tomcat-7.0.62\webapps\jyxt\] instead of [C:\App\Tomcat\apache-tomcat-7.0.62\webapps\wmsnew\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
	at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:150)
	at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:116)
	at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)
	at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

异常明显提示:

       Web app root system property already set to different value: 'webapp.root'

网上查了一下,发生的原因是:当两个web应用在同一容器中定义了相同的webAppRootKey或者都没有定义,则需要为每个web都定义一个webAppRootKey


解决的办法就是,修改web.xml文档,指定工程自己的webAppRootKey


         ### app1:
<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>app1.root</param-value>
</context-param>

### app2:
<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>app2.root</param-value>
</context-param>

        修改配置后重启tomcat,则2个项目都能正常访问到了。


-----------------------------------webAppRootKey作用补充------------------------------------

web.xml中webAppRootKey


1、 web.xml配置 
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
"webapp.root"这个字符串可以随便写任何字符串。如果不配置默认值是"webapp.root"。
 
可以用System.getProperty("webapp.root")来动态获项目的运行路径。
一般返回结果例如:/usr/local/tomcat6/webapps/项目名

2、解决以下报错

部署在同一容器中的Web项目,要配置不同的<param-value>,不能重复,否则报类似下面的错误:
Web app root system property already set to different value: 'webapp.root' = [/home/user/tomcat/webapps/project1/] instead of [/home/user/tomcat/webapps/project2/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!  
意思是“webapp.root”这个key已经指向了项目1,不可以再指向项目2.

3、加载方式

Spring通过org.springframework.web.util.WebAppRootListener 这个监听器来运行时的项目路径。
但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener这个监听器,
则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能
一般配置类型下面的例子:

Xml代码   收藏代码
  1. <!-- 加载Log4J 配置文件  -->  
  2. <context-param>  
  3.     <param-name>log4jConfigLocation</param-name>  
  4.     <param-value>WEB-INF/conf/log4j.properties</param-value>  
  5. </context-param>     
  6.   
  7. <context-param>  
  8.     <param-name>log4jRefreshInterval</param-name>  
  9.       <param-value>3000</param-value>  
  10.  </context-param>  
  11.   
  12. <listener>  
  13.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  14.  </listener>  

 

4、在运行时动态的找出项目的路径

在log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:
 log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log 
就可以在运行时动态的找出项目的路径,并且把log文件放到webapp中中。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值