xml Spring 中的 WebApproot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5014651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebApproot in Spring
提问by michaelHymanson4ever
I get this error message
我收到此错误消息
[ SEVERE: 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:\Users\jaanlai\Documents\NetBeansProjects\absSovellus\build\web] instead of [C:\Users\Administrator\Documents\NetBeansProjects\keycard2\build\web] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
[严重:异常将上下文初始化事件发送到类 org.springframework.web.util.Log4jConfigListener java.lang.IllegalStateException:Web 应用程序根系统属性已设置为不同的值:'webapp.root' = [C:\Users \jaanlai\Documents\NetBeansProjects\absSovellus\build\web] 而不是 [C:\Users\Administrator\Documents\NetBeansProjects\keycard2\build\web] - 为 web.xml 中的“webAppRootKey”上下文参数选择唯一值文件!
It's odd, because I don't have any webAppRootKey defined in my files. What is it?
这很奇怪,因为我的文件中没有定义任何 webAppRootKey。它是什么?
回答by mblinn
The webAppRootKeyis a context parameter that Spring uses in a couple of places. In this case, it's being used by the Log4jWebConfigurer. It exposes the webapp root as a system property that can be used in log4j configuration files, something like this:
这webAppRootKey是 Spring 在几个地方使用的上下文参数。在这种情况下,它被Log4jWebConfigurer. 它将 webapp 根公开为可在 log4j 配置文件中使用的系统属性,如下所示:
log4j.appender.testfile.File=${webapp.root}/WEB-INF/testlog.log
You would use this if you, for some reason, wanted to locate your logs relative to your webapp root.
如果您出于某种原因想要相对于您的 webapp 根定位您的日志,您将使用它。
The problem that you're running into is that some containers (notably Tomcat) don't maintain a per-webapp mapping of system properties. When you don't specify a webAppRootKey, Spring defaults it to webapp.root. Since you're running two apps in the same container, the second app you're trying to start up sees that the webAppRootKeyis already set (via the default), and throws an error. Otherwise, the webAppRootKeywould be set incorrectly, and you could end up with logs from one webapp in another webapp.
您遇到的问题是某些容器(尤其是 Tomcat)不维护系统属性的每个 webapp 映射。当您未指定 a 时webAppRootKey,Spring 会将其默认为webapp.root。由于您在同一个容器中运行两个应用程序,因此您尝试启动的第二个应用程序看到webAppRootKey已设置(通过默认设置),并引发错误。否则,webAppRootKey将被错误地设置,并且您最终可能会得到来自另一个 webapp 中的一个 webapp 的日志。
You can specify a different webAppRootKeyusing context parameters in your web.xmllike so:
您可以像这样指定不同的webAppRootKeyusing 上下文参数web.xml:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root.one</param-value>
</context-param>
And
和
log4j.appender.testfile.File=${webapp.root.one}/WEB-INF/testlog.log
in your log4j. This should take care of the conflict.
在你的 log4j 中。这应该处理冲突。
回答by MikeRoger
<context-param>
<param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>
...
...
This solved the problem for me. Credit to:- http://forum.springsource.org/archive/index.php/t-32873.html
这为我解决了这个问题。归功于:- http://forum.springsource.org/archive/index.php/t-32873.html
回答by axtavt
It looks like you have several webapps with default Log4jConfigListenerconfiguration in your application server.
看起来Log4jConfigListener您的应用程序服务器中有几个具有默认配置的web应用程序。
Default behaviour for Log4jConfigurationListeneris to expose webapp root as a system property named webapp.root, to allow you to use it when specifying log file locations. However, if system property with the same name already exists, it throws an exception.
默认行为Log4jConfigurationListener是将 webapp root 公开为名为 的系统属性webapp.root,以允许您在指定日志文件位置时使用它。但是,如果已存在同名的系统属性,则会引发异常。
You can either configure per-application names for that system property using <context-param>named webAppRootKey, or disable exposing of the system property by setting Log4jConfigListener's <init-param>named log4jExposeWebAppRootto false.
您可以使用<context-param>named为该系统属性配置每个应用程序的名称webAppRootKey,或者通过将Log4jConfigListener的<init-param>named设置为log4jExposeWebAppRoot来禁用系统属性的公开false。
See also:
也可以看看:
回答by Keeg
Just in case anyone else has done the above without getting rid of the problem:
以防万一其他人在没有解决问题的情况下完成了上述操作:
Our webapp had the webAppRootKey set correctly, but I still got the exception above. Restarting Glassfish and redeploying the same war-file worked, go figure.
我们的 webapp 正确设置了 webAppRootKey,但我仍然遇到上述异常。重新启动 Glassfish 并重新部署相同的War文件,看看吧。
回答by code4kix
If you use logbackinstead of log4jand get the same error, this solves it:
如果您使用logback而不是log4j并得到相同的错误,这可以解决它:
<context-param>
<param-name>logbackExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>

