SLF4J: Class path contains multiple SLF4J bindings 问题解决

背景:

springboot项目名称 test,在使用slf4j后,服务启动报错

 

报错信息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Program%20Files/Java/.m2/repository/ch/qos/logback/logback-classic/1.2.7/logback-classic-1.2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Program%20Files/Java/.m2/repository/org/slf4j/slf4j-log4j12/1.7.32/slf4j-log4j12-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

 

报错原因:

以上报错原因是包引用冲突,出现多个slf4j的实现。

logback-classic-1.2.7.jar 和 slf4j-log4j12-1.7.32.jar 冲突

查看SLF4J官方给出的解决冲突的方法:

http://www.slf4j.org/codes.html#multiple_bindings

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

  </dependency>
</dependencies>

 

解决问题:

1 找到产生冲突的依赖

方法1:

打开终端,在test目录下运行下面命令,显示服务的所有依赖

mvn dependency:tree

查找到 slf4j-log4j12的引用,这里是zookeeper中的引用冲突了

[INFO] +- org.apache.zookeeper:zookeeper:jar:3.6.1:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- org.apache.zookeeper:zookeeper-jute:jar:3.6.1:compile
[INFO] |  +- org.apache.yetus:audience-annotations:jar:0.5.0:compile
[INFO] |  +- io.netty:netty-transport-native-epoll:jar:4.1.70.Final:compile
[INFO] |  |  +- io.netty:netty-transport-native-unix-common:jar:4.1.70.Final:compile
[INFO] |  |  \- io.netty:netty-transport-classes-epoll:jar:4.1.70.Final:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.32:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile

 

方法2:

 使用idead的Maven工具栏-》点击启动失败的服务test-》点击两个上箭头标识的“显示图”

放大依赖关系图,查找 slf4j-log4j12 位置,找到上级依赖

zookeeper -> slf4j-log4j12

 

2 将依赖中的slf4j排除

根据步骤1找到的冲突依赖是zookeeper中的引用出现冲突了,加入排除配置

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.6.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

总结:

出现以上问题的原因是依赖冲突,我们解决问题分两步:

1. 找到冲突包所在位置

2. 排除冲突的依赖

 

posted on 2024-07-22 16:59  欢跳的心  阅读(1030)  评论(0编辑  收藏  举报