slf4j-api 1.6.x (or later) is incompatible with this binding. Solution and principle

When I built the project a few days ago, I always reported this exception every time I started:

[java]  view plain copy  
 
  View code snippets on CODE Derive to my code slice
  1. SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.  
  2. SLF4J: Your binding is version 1.5.5 or earlier.  
  3. SLF4J: Upgrade your binding to version 1.6.x. or 2.0.x  


Then followed by:

[java]  view plain copy  
 
  View code snippets on CODE Derive to my code slice
  1. 10:02:47.127:WARN::Nested in java.lang.ExceptionInInitializerError:  
  2. java.lang.IllegalStateException: org.slf4j.LoggerFactory could not be successfully initialized. See also http://www.slf4j.org/codes.html#unsuccessfulInit  
  3.     at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:277)  
  4.     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:241)  
  5.     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)  
  6.     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)  
  7.     at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:272)  
  8.     at org.springframework.web.context.ContextLoader.<clinit>(ContextLoader.java:149)  
  9.     at com.alibaba.citrus.webx.context.WebxContextLoaderListener.createContextLoader(WebxContextLoaderListener.java:40)  
  10.     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:44)  
  11.     at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)  
  12.     at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)  
  13.     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)  
  14.     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)  
  15.     at org.mortbay.jetty.webapp.WebAppContext.doStart (WebAppContext.java: 467 )  
  16.     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)  
  17.     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)  
  18.     at org.mortbay.jetty.Server.doStart(Server.java:224)  
  19.     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)  
  20.     at runjettyrun.Bootstrap.main(Bootstrap.java:259)  



从错误提示来看,是版本不兼容的问题(废话)。

 首先估计是slf4j-api的版本,在每个子工程下面不一样,也许在子工程1里面是1.6.1,在子工程2里面是1.5.6,打包运行时候,出现了版本冲突导致的。

eclipse工程下——打开pom文件——选择dependecy Hierarchy ,就可以查看每个jar包以及他们之间的依赖关系。(本质上执行mvn dependency:tree命令是一样的)

   查看某个工程的Pom文件,发现其中一个的Pom文件依赖关系是这样的:


其中,当我们在右上角搜索slf的时候,右边显示的是所有的jar包,橙色被选中的是匹配的同名Jar包,我们可以看到有slf4j-api:1.5.6,slf4j-log4j12:1.4.3以及jcl-over-slf4j:1.5.6;左边是工程中哪些其他Jar包间接依赖了slf4j-*,其中compile表示该版本为选中的编译版本,omitted for confict with ××× 表示因为存在×××版本,当前版本被忽略。

因此我们可以看到,slf4j-api编译版本为1.5.6,slf4j-log12编译版本为1.4.3,jcl-over-slf4j编译版本为1.5.6。如果工程中所有依赖的slf4j-api版本都是1.5.6也是允许的,但是考虑到工程整体结构,以及高版本通常兼容低版本,最重要的是只有这个一个子工程用了低版本,当然要升级到1.6.1了!

在此再罗嗦一下,简单说一下Mvn工程Pom文件中dependency之间各jar包版本的依赖关系:

A. pom文件的依赖按声明顺序上到下读取,即先声明的优先

    例如:pom文件中依赖的声明顺序由上向下为: 

dependency:A(1.0.1)

dependency:A(1.0.2)

那么系统依赖肯定是依赖A1.0.1,因为该依赖最先被声明,后面的被Omitted(忽略)

B. 对于间接依赖,采用最短路径优先算法,距离短的被采用, 其中“—”表示间接依赖

AB—C—D(1.0.0)

F—E—D(1.0.1)

其中A间接依赖B,B又间接依赖C,C又间接依赖D,此时路径距离为3;另一个依赖:F间接依赖E,E间接依赖D,此时路径为2;

根据最短路径优先原则,会依赖F-E-D(1.0.1)中的D(1.0.1)版本。

 

C. 对于间接依赖路径长度相同的,谁先声明选谁,和A的规则是一样的

A—B—C—D(1.0.0)

F—E—K—D(1.0.1)

If the path length is the same, the one that declares first will be selected first, and D(1.0.0) must be selected.

 

In summary:

     If we want to upgrade slf4j-api to 1.6.1, the most direct way is to declare a version of slf4j-api:1.6.1 directly before the declaration of tair-mc-client, or exclude tair-mc-client --tair Dependency of -cient --slf4j-api:1.5.6, which indirectly depends on tair-mc-client --tair-client --echache --slf4j-api:1.6.1. In order to be intuitive, we choose to directly declare a dependency of slf4j-api: 1.6.1 version. At the same time, in order to ensure the consistency of slf4j-api and slf4j-log4j12 versions, the slf4j-log4j12:1.6.1 version is directly introduced, because slf4j-log4j12:1.6.1 indirectly depends on slf4j:1.6.1, and declare it at the top of the file, Problem solved .

 

For excluding conflicts between multiple versions of the same jar package:

The first is to figure out the dependencies between the various Jar packages: for the mvn project using the eclipse tool, you can directly view the dependency Hierarchy in the pom file or use the command mvn dependency:tree to display the dependencies, so that you know which indirect version is something wrong;

        Then select the appropriate version and eliminate the unwanted version: usually the higher version is preferred, and a higher version can be declared in a relatively high position in the Pom file, or a lower version dependency can be excluded;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154840&siteId=291194637