如何在JBoss上为Spring配置Log4j

如何在JBoss上为Spring配置Log4j,spring,jboss,log4j,Spring,Jboss,Log4j,我正试图在我的JBoss5.1.0GA服务器上部署SpringMVC+tiles演示项目。我在Log4J中遇到了这个问题: 02:35:49,443 ERROR [StandardContext] Error listenerStart 02:35:49,443 ERROR [StandardContext] Context [/travel] startup failed due to previous errors 02:35:49,443 INFO [[/travel]] Clo

我正试图在我的JBoss5.1.0GA服务器上部署SpringMVC+tiles演示项目。我在Log4J中遇到了这个问题:

    02:35:49,443 ERROR [StandardContext] Error listenerStart
02:35:49,443 ERROR [StandardContext] Context [/travel] startup failed due to previous errors
02:35:49,443 INFO  [[/travel]] Closing Spring root WebApplicationContext
[Loaded org.springframework.web.context.ContextCleanupListener from jar:file:/opt/jboss-5.1.0.GA/server/default/deploy/travel.war!/WEB-INF/lib/spring-web-3.0.5.RELEASE.jar]
02:35:49,444 ERROR [[/travel]] Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.LogManager
        at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:249)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:131)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:645)
        at org.springframework.web.context.ContextCleanupListener.<clinit>(ContextCleanupListener.java:43)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:80)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:3949)
        at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4615)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4437)
        at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:310)
        at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:142)
        at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
        at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118)
        at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
        at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:206)
        at $Proxy38.start(Unknown Source)
        at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:42)
        at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:37)
        at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Enables use of HTTP methods PUT and DELETE -->
    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Handles all requests into the application -->
    <servlet>
        <servlet-name>travel</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/META-INF/spring/travel/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>travel</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

我想这可能是因为类路径上有两个log4j.jar,但如何检查?

我认为,首先,如果怀疑类路径中有多个log4j jar,可以将参数-verbose:class添加到JBoss中,以检查实际加载了哪个org.apache.log4j.LogManager。它将打印出所有类加载信息。确定,找到两个:[从文件中加载org.apache.log4j.LogManager:/opt/jboss-5.1.0.GA/lib/log4j boot.jar][从jar中加载org.apache.log4j.LogManager:file:/opt/jboss-5.1.0.GA/server/default/deploy/travel.war!/WEB-INF/lib/log4j-1.15.jar]在Maven build中的pom.xml文件中,我有log4j的“运行时”范围。我应该将log4j从war中删除还是从JBoss中删除?这里的方法是什么?将其范围更改为“提供”,至少在我的公司这是一个建议。然而,它可能需要更多的配置才能让Jboss的日志与你的应用和平共处。看看这里或者谷歌其他类似的文章,它们应该会有所帮助@泽克