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.jar
的 StaticLoggerBinder.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>