与Tomcat和WebApplicationInitializer不匹配的slf4j版本

时间:2013-09-04 16:06:42

标签: java spring tomcat7 gradle slf4j

当我尝试在Tomcat上部署我的应用程序时,我遇到以下错误。

04-Sep-2013 17:59:29.424 SEVERE [AsyncFileHandlerWriter-1626573634] org.apache.catalina.core.StandardContext.startInternal Error during ServletContainerInitializer processing
 javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:165)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5370)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/springframework/instrument/classloading/tomcat/TomcatInstrumentableClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:299)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
    at org.springframework.web.context.AbstractContextLoaderInitializer.<init>(AbstractContextLoaderInitializer.java:42)
    at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.<init>(AbstractDispatcherServletInitializer.java:58)
    at org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer.<init>(AbstractAnnotationConfigDispatcherServletInitializer.java:40)
    at com.bravofly.wfcobrand.configuration.web.ServletConfiguration.<init>(ServletConfiguration.java:10)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:162)
    ... 13 more

我已经发现问题出在lib目录中的库中:slf4j-api-1.5.11.jar。

放入org.slf4j:slf4j-api:1.7.1org.slf4j:jcl-over-slf4j:1.7.1解决了我的问题。

我想知道是否可以在我的应用中做一些事情来解决问题。我正在使用gradle所以我把:

  compile ... other dependencies
          "org.slf4j:slf4j-api:1.7.1",
          "org.slf4j:jcl-over-slf4j:1.7.1"

但这并没有解决问题。有没有办法优先考虑我的构建文件中定义的库?

1 个答案:

答案 0 :(得分:3)

虽然我还不确定原因,但是共享lib目录中的servlet容器是另一个项目的旧版本。所以当我们删除它时一切正常。

我们正在使用Tomcat 7,我相信app libs具有优先权,但显然如果已经加载了它的版本,它就会丢弃我们的。