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
包,關于org/slf4j/impl/StaticLoggerBinder.class
這個類發(fā)生了沖突。
發(fā)生這個錯誤的原因,首先logback
日志的開發(fā)者和log4j
的開發(fā)者據(jù)說是一波人,而springboot
默認日志是,較新的logback
日志。
但是在以前流行的日志卻是log4j
,而且很多的第三方工具都含有log4j
得引入。
而我們在項目開發(fā)中,難免會引入各種各樣的工具包,所以,基本上springboot
項目,如果不注意,肯定會出現(xiàn)這種沖突的。
問題隱患
當然最關心的是它是否有隱患,如果你在開發(fā)工具中運行,對,沒毛病,一般會正常啟動。
經(jīng)過我使用情況中的觀察,貌似springboot
配置成tomcat運行
,即修改成war
包之后,一般這個警告沒有什么影響;但是如果是傳統(tǒng)的jar
包,盡管你在開發(fā)工具中能正常運行,也可能在打完包之后不能運行。
問題出現(xiàn)
因為我們是分布式項目開發(fā),服務層作為一個jar
運行,而接口調用和前端頁面,作為一個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
的原因,你們也可能不關心這個,我們只想知道如何解決這種問題而已。
問題解決辦法很簡單,就是既然拋了jar包沖突 ,那我們就排除一個jar
包即可。
關鍵是排除哪一個jar包
,這里注意下了,如果你用的是logback
日志,一定要排除slf4j-log4j12
包,不要排除logback-classic
包。
即找到pom.xml
文件,如果你們的開發(fā)工具,比如eclipse
和idea
都可以看引入jar
包的聯(lián)系,比如idea可以這樣看到你的依賴結構:
點擊后,彈出下邊的這樣的結構:
通過上圖中,你可以看到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>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springBoot熱部署、請求轉發(fā)與重定向步驟詳解
這篇文章主要介紹了springBoot熱部署、請求轉發(fā)與重定向,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06java比較器Comparable接口與Comaprator接口的深入分析
本篇文章是對java比較器Comparable接口與Comaprator接口進行了詳細的分析介紹,需要的朋友參考下2013-06-06Mybatis?Plus使用XML編寫動態(tài)sql的超簡易方法
這篇文章主要介紹了Mybatis?Plus使用XML編寫動態(tài)sql的超簡易方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01java開發(fā)使用StringUtils.split避坑詳解
這篇文章主要為大家介紹了java開發(fā)使用StringUtils.split避坑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11