springboot解決Class path contains multiple SLF4J bindings問題
解決Class path contains multiple SLF4J bindings.警告
有一次配置好springboot項目啟動后,
忽然發(fā)現(xiàn)有下邊的警告:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/org/slf4j/slf4j-log4j12/1.7.22/slf4j-log4j12-1.7.22.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]
原因分析
上邊的大概意思是說logback-classic 包和slf4j-log4j12 包,關(guān)于org/slf4j/impl/StaticLoggerBinder.class 這個類發(fā)生了沖突。
發(fā)生這個錯誤的原因,首先logback 日志的開發(fā)者和log4j 的開發(fā)者據(jù)說是一波人,而springboot 默認日志是,較新的logback 日志。
但是在以前流行的日志卻是log4j ,而且很多的第三方工具都含有log4j 得引入。
而我們在項目開發(fā)中,難免會引入各種各樣的工具包,所以,基本上springboot 項目,如果不注意,肯定會出現(xiàn)這種沖突的。
問題隱患
當然最關(guān)心的是它是否有隱患,如果你在開發(fā)工具中運行,對,沒毛病,一般會正常啟動。
經(jīng)過我使用情況中的觀察,貌似springboot 配置成tomcat運行 ,即修改成war 包之后,一般這個警告沒有什么影響;但是如果是傳統(tǒng)的jar 包,盡管你在開發(fā)工具中能正常運行,也可能在打完包之后不能運行。
問題出現(xiàn)
因為我們是分布式項目開發(fā),服務(wù)層作為一個jar 運行,而接口調(diào)用和前端頁面,作為一個war 一起運行,也就是說我們即有war,又有jar ,項目部署的時候,需要打完包之后運行才可以。
在打完包之后,war 包能正常運行,jar 包不能正常運行,報了如下錯誤:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)Caused by: java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45
)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:155)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:132)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication
.java:190)
at spingboot.study.SpringbootStudyApplication.main(SpringbootStudyApplic
ation.java:14)
... 8 more
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar A
ND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See
also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54
)
... 19 more
問題解決
當然問題我不敢確定一定是因為war 和jar 的原因,你們也可能不關(guān)心這個,我們只想知道如何解決這種問題而已。
問題解決辦法很簡單,就是既然拋了jar包沖突 ,那我們就排除一個jar 包即可。
關(guān)鍵是排除哪一個jar包 ,這里注意下了,如果你用的是logback 日志,一定要排除slf4j-log4j12 包,不要排除logback-classic 包。
即找到pom.xml 文件,如果你們的開發(fā)工具,比如eclipse 和idea 都可以看引入jar 包的聯(lián)系,比如idea可以這樣看到你的依賴結(jié)構(gòu):

點擊后,彈出下邊的這樣的結(jié)構(gòu):

通過上圖中,你可以看到zookeeper 包中默認引入了slf4j-log4j12包,除此之外,還有我們springboot 一定引入的spring-boot-starter-web 包,它里邊也有這個slf4j-log4j12 引入。 我們只要在pom.xml 里排除這個即可。
如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除這個slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>下邊是我們項目引入的第三方的工具包中:
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!--排除這個slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot結(jié)合JWT實現(xiàn)單點登錄的示例
本文主要介紹了springboot結(jié)合JWT實現(xiàn)單點登錄的示例,包括生成Token、驗證Token及使用Redis存儲Token,具有一定的參考價值,感興趣的可以了解一下2025-01-01
SpringBoot整合Guava Cache實現(xiàn)全局緩存的示例代碼
這篇文章主要介紹了SpringBoot整合Guava Cache實現(xiàn)全局緩存,Guava Cache是Google Guava庫中的一個模塊,提供了基于內(nèi)存的本地緩存實現(xiàn),文中介紹了SpringBoot整合使用Guava Cache的具體步驟,需要的朋友可以參考下2024-03-03

