4、 打包后Log4j支持问题
打包成.war部署到WebLogic后,出现如下问题:
Error: weblogic.management.DeploymentException: Cannot set web app root system property when WAR file is not expanded - with nested exception:
[java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded]
问题解决:通常您不需要亲自编写servlet或者listener,比如直接利用log4j的com.apache.jakarta.log4j.Log4jInit类,Spring的org.springframework.web.util.Log4jConfigServlet和org.springframework.web.util.ServletContextListener方式配置,找到.Log4jConfigServlet和ServletContextListener的源码,他们都在适当的地方(callback method)调用了Log4jWebConfigurer.initLogging(getServletContext());定位到这个方法,第一句就是:WebUtils.setWebAppRootSystemProperty(servletContext);再定位到该方法,方法很短:
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
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) {
throw new IllegalStateException("WARNING: Web app root system property already set: " + key + " = " + oldValue + " - Choose unique webAppRootKey values in your web.xml files!");
}
String root = servletContext.getRealPath("/");
if (root == null) {
throw new IllegalStateException("Cannot set web app root system property when WAR file is not expanded");
}
System.setProperty(key, root);
servletContext.log("Set web app root system property: " + key + " = " + root);
}
系统需要读取webAppRootKey这个参数,所以在部署到WebLogic里的时候,在web.xml中手动添加如下代码:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>