war包部署weblogic的Log4j支持问题

本文来自:http://www.codesky.net/article/200707/119054.html

工程打包成.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>


WebLogic自身也包含对Log4j的支持,在打包部署(.war)的时候,会和Spring的org.springframework.web.util.Log4jConfigListener有冲突(拷贝到WebLogic散放部署不会出错)。所以改用Servlet加载。(不通过应用加载Log4j好像也可以使用,但未进行完整测试,下面代码修改后,系统会报Log4j加载重复错误,不影响应用启动。)

web.xml中删除下面代码:

<listener id="log4jConfigListener">

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

</listener>

将Listener加载改为通过Servlet加载,再在web.xml增加:

<servlet>

<servlet-name>log4jConfigListener</servlet-name>

<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>

<load-on-startup>0</load-on-startup>

</servlet>

猜你喜欢

转载自haroldxie.iteye.com/blog/1751125
今日推荐