解决 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

解决 SLF4J: Class path contains multiple SLF4J bindings._第1张图片

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/** ,避免打包进来。


    org.apache.maven.plugins
     maven-shade-plugin
     2.4.3
     
         
             package
             
                 shade
             
             
                 
                     
                         *:*
                         
                             META-INF/*.SF
                             META-INF/*.DSA
                             META-INF/*.RSA
                             org/springframework/**
                             org/slf4j/**
                         
                     
                 
                 
                     
                         
                     
                 
             
         
     
 

4. 总结

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

<dependency>
    <groupId>com.midea.ottergroupId>
    <artifactId>client.coreartifactId>
    <version>4.2.19-SNAPSHOTversion>
    <exclusions>
        <exclusion>
        	<groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        exclusion>
    exclusions>
dependency>

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

<dependency>
    <groupId>org.apache.zookeepergroupId>
    <artifactId>zookeeperartifactId>
    <version>${zookeeper.version}version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        exclusion>
    exclusions>
dependency>

你可能感兴趣的:(maven依赖管理,java,maven)