java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext问题原因及解决方法

一、错误信息

java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
at ch.qos.logback.ext.spring.LogbackConfigurer.initLogging(LogbackConfigurer.java:72)
at ch.qos.logback.ext.spring.web.WebLogbackConfigurer.initLogging(WebLogbackConfigurer.java:142)
at ch.qos.logback.ext.spring.web.LogbackConfigListener.contextInitialized(LogbackConfigListener.java:54)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
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:744)

二、检查日志中是否包含如下信息

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/httx/run/tomcat/webapps/cashBillSite/WEB-INF/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/httx/run/tomcat/webapps/cashBillSite/WEB-INF/lib/logback-classic-1.0.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

三、问题原因

LogbackConfigurer在做初始化时,需要对返回LogggerContext做强转;在这里报错,报错原因是 slf4j-log4j12-1.6.4.jar和logback-classic-1.0.9.jar的有StaticLoggerBinder冲突,这里如果运气好,他加载的是logback-classic-1.0.9.jar的StaticLoggerBinder就不会报错,如果运气不好,加载slf4j-log4j12-1.6.4.jar中的StaticLoggerBinder就悲剧了,无法强转。

  slf4j-log4j12-1.6.4.jar和logback-classic-1.0.9.jar中StaticLoggerBinder路径一样,所以加载容易出错

 

四、解决方法

修改pom.xml文件,去掉对slf4j-log4j12-1.6.4.jar的引用

<dependency>
<groupId>tf56</groupId>
<artifactId>risk</artifactId>
<version>1.0.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

原文:http://www.cnblogs.com/simplelei/p/5408029.html

posted on 2016-12-09 13:50  沉风丶  阅读(822)  评论(0编辑  收藏  举报

导航