springboot 關於 Class path contains multiple SLF4J bindings.
警告的解決
有一次配置好springboot項目啟動后,忽然發現有下邊的警告:
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
這個類發生了沖突。
發生這個錯誤的原因,首先logback
日志的開發者和log4j
的開發者據說是一波人,而springboot
默認日志是,較新的logback
日志。但是在以前流行的日志卻是log4j
,而且很多的第三方工具都含有log4j
得引入。
而我們在項目開發中,難免會引入各種各樣的工具包,所以,基本上springboot
項目,如果不注意,肯定會出現這種沖突的。
問題隱患:
當然最關心的是它是否有隱患,如果你在開發工具中運行,對,沒毛病,一般
會正常啟動。
經過我使用情況中的觀察,貌似springboot
配置成tomcat運行
,即修改成war
包之后,一般這個警告沒有什么影響;但是如果是傳統的jar
包,盡管你在開發工具中能正常運行,也可能在打完包之后不能運行。
問題出現:
因為我們是分布式項目開發,服務層作為一個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
文件,如果你們的開發工具,比如eclipse
和idea
都可以看引入jar
包的聯系,比如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>