解决 SLF4J: Class path contains multiple SLF4J bindings.

1. 异常现象

启动springboot项目,抛出警告信息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/quanll5/Documents/java_repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/quanll5/Documents/java_code/datahup/replicator/client.core-4.2.19-SNAPSHOT.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]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2. 排查分析

从警告信息来来看,有2个jar包都出现了 /org/slf4j/impl/StaticLoggerBinder.class 这个类,于是发生了冲突,不知道绑定哪个

这2个jar包分别是:

  • logback-classic-1.2.3.jar
  • client.core-4.2.19-SNAPSHOT.jar

client.core-4.2.19-SNAPSHOT.jar 是我打包的SDK,解压打开,确实有这么一个类 /org/slf4j/impl/StaticLoggerBinder.class

在这里插入图片描述

3. 解决方案

只能选择其中一个jar包的/org/slf4j/impl/StaticLoggerBinder.class

我选择保留 logback-classic-1.2.3.jar,怎么去掉 client.core-4.2.19-SNAPSHOT.jarStaticLoggerBinder.class ?

那就要设置一下maven打包插件,在 maven-shade-plugin 插件用 exclude 排除 org/slf4j/** ,避免打包进来。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>2.4.3</version>
     <executions>
         <execution>
             <phase>package</phase>
             <goals>
                 <goal>shade</goal>
             </goals>
             <configuration>
                 <filters>
                     <filter>
                         <artifact>*:*</artifact>
                         <excludes>
                             <exclude>META-INF/*.SF</exclude>
                             <exclude>META-INF/*.DSA</exclude>
                             <exclude>META-INF/*.RSA</exclude>
                             <exclude>org/springframework/**</exclude>
                             <exclude>org/slf4j/**</exclude>
                         </excludes>
                     </filter>
                 </filters>
                 <transformers>
                     <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                         <mainClass></mainClass>
                     </transformer>
                 </transformers>
             </configuration>
         </execution>
     </executions>
 </plugin>

4. 总结

由于 client.core-4.2.19-SNAPSHOT.jar 是我打包的,把三方依赖的源码也包进来了,适合打包阶段用maven插件排除然后被别的项目使用,也就是上述方案。反而不适合别的项目用exclusion排除,因为排不掉:

<dependency>
    <groupId>com.midea.otter</groupId>
    <artifactId>client.core</artifactId>
    <version>4.2.19-SNAPSHOT</version>
    <exclusions>
        <exclusion>
        	<groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

如果你是引入ZK等依赖,它们内部用pom文件管理三方依赖,就适合这种方式,举个例子:

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>${zookeeper.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值