webAppRootKey

Spring MVC 中的web.xml 中这个配置是什么意思呢?

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

查找资料后这个是由Spring 定义的一个值

具体作用在源码中:

/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Listener that sets a system property to the web application root directory.
 * The key of the system property can be defined with the "webAppRootKey" init
 * parameter at the servlet context level (i.e. context-param in web.xml),
 * the default key is "webapp.root".
 *
 * <p>Can be used for toolkits that support substition with system properties
 * (i.e. System.getProperty values), like log4j‘s "${key}" syntax within log
 * file locations.
 *
 * <p>Note: This listener should be placed before ContextLoaderListener in <code>web.xml</code>,
 * at least when used for log4j. Log4jConfigListener sets the system property
 * implicitly, so there‘s no need for this listener in addition to it.
 *
 * <p><b>WARNING</b>: Some containers, e.g. Tomcat, do NOT keep system properties separate
 * per web app. You have to use unique "webAppRootKey" context-params per web app
 * then, to avoid clashes. Other containers like Resin do isolate each web app‘s
 * system properties: Here you can use the default key (i.e. no "webAppRootKey"
 * context-param at all) without worrying.
 *
 * <p><b>WARNING</b>: The WAR file containing the web application needs to be expanded
 * to allow for setting the web app root system property. This is by default not
 * the case when a WAR file gets deployed to WebLogic, for example. Do not use
 * this listener in such an environment!
 *
 * @author Juergen Hoeller
 * @since 18.04.2003
 * @see WebUtils#setWebAppRootSystemProperty
 * @see Log4jConfigListener
 * @see java.lang.System#getProperty
 */
public class WebAppRootListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        WebUtils.setWebAppRootSystemProperty(event.getServletContext());
    }

    public void contextDestroyed(ServletContextEvent event) {
        WebUtils.removeWebAppRootSystemProperty(event.getServletContext());
    }

}
    /**
     * Set a system property to the web application root directory.
     * The key of the system property can be defined with the "webAppRootKey"
     * context-param in <code>web.xml</code>. Default is "webapp.root".
     * <p>Can be used for tools that support substition with <code>System.getProperty</code>
     * values, like log4j‘s "${key}" syntax within log file locations.
     * @param servletContext the servlet context of the web application
     * @throws IllegalStateException if the system property is already set,
     * or if the WAR file is not expanded
     * @see #WEB_APP_ROOT_KEY_PARAM
     * @see #DEFAULT_WEB_APP_ROOT_KEY
     * @see WebAppRootListener
     * @see Log4jWebConfigurer
     */
    public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
        Assert.notNull(servletContext, "ServletContext must not be null");
        String root = servletContext.getRealPath("/");
        if (root == null) {
            throw new IllegalStateException(
                "Cannot set web app root system property when WAR file is not expanded");
        }
        String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
        String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
        String oldValue = System.getProperty(key);
        if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
            throw new IllegalStateException(
                "Web app root system property already set to different value: ‘" +
                key + "‘ = [" + oldValue + "] instead of [" + root + "] - " +
                "Choose unique values for the ‘webAppRootKey‘ context-param in your web.xml files!");
        }
        System.setProperty(key, root);
        servletContext.log("Set web app root system property: ‘" + key + "‘ = [" + root + "]");
    }

它的作用就是

System.setProperty(key,root);

key 的默认值 "webapp.root"

给这个key 换一个名字

root 是 servletContext.getRealPath("/")

然后其他地方就可以使用啦! 包括log4j的配置文件中为文件指定位置,或者其他类中使用这个系统变量。

webapp.root=根目录

通过这个就可以变成

myfirstapp.root=根目录

每个项目都用各自的,避免混淆

官方资料

时间: 2024-10-20 10:10:34

webAppRootKey的相关文章

Choose unique values for the &#39;webAppRootKey&#39; context-param in your web.xml files!

在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下: appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very lik

web.xml中webAppRootKey

------------------------------------------------------------------------------------------------1. web.xml配置  <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param>

tomcat中部署多个项目,webAppRootKey 参数配置

在一个tomcat中部署多个项目时,需要在每个项目的web.xml中配置webAppRootKey参数,如下: <context-param> <param-name>webAppRootKey</param-name> <param-value>{your web app}.root</param-value> </context-param>

Log4j2 与Spring 使用webAppRootKey

    <context-param>         <param-name>webAppRootKey</param-name>         <param-value>webApp.root</param-value>     </context-param>     <context-param>         <param-name>log4jConfigLocation</param-na

[转] Log4j 配置 的webAppRootKey参数问题

在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下:<!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param> 最好保持每个项目的参数

[转]web.xml中webAppRootKey

web.xml中webAppRootKey ------------------------------------------------------------------------------------------------1. web.xml配置 <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value>

log4j和web.xml配置webAppRootKey 的问题

在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为"webapp.root",如下: <!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param>

(转)web.xml中webAppRootKey

web.xml中webAppRootKey ------------------------------------------------------------------------------------------------ 1. web.xml配置 webAppRootKey webapp.root "webapp.root"这个字符串可以随便写任何字符串.如果不配置默认值是"webapp.root". 可以用System.getProperty(&q

Log4j 配置 的webAppRootKey参数问题

为了让Web项目中的Spring 使用Log4j做如下配置: 1.在web.xml中添加如下内容: <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root".但最好设置,以免项目之间的名称冲突. 如我这里有两个项目都在web.xml都没配置webAppRootKey,那么这两个项目发布到tomcat时就会出现如下的异常: Xml代码 严重: Exception sending context initialized ev