1. 程式人生 > 實用技巧 >springboot 關於 Class path contains multiple SLF4J bindings.警告的解決

springboot 關於 Class path contains multiple SLF4J bindings.警告的解決

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

問題解決:

  當然問題我不敢確定一定是因為warjar的原因,你們也可能不關心這個,我們只想知道如何解決這種問題而已。
  問題解決辦法很簡單,就是既然拋了jar包衝突,那我們就排除一個jar包即可。關鍵是排除哪一個jar包,這裡注意下了,如果你用的是logback日誌,一定要排除slf4j-log4j12包,不要排除logback-classic包。
  即找到pom.xml檔案,如果你們的開發工具,比如eclipseidea都可以看引入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>