今天啓動項目的時候出現jar包依賴衝突的問題,spring-boot項目默認使用logback日誌庫,然而又引入了log4j2導致出現下面的錯誤
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/86186/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/86186/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.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 [org.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread "main" java.lang.StackOverflowError
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:112)
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:125)
at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
解決方法
- 在Idea中查看依賴關係圖
點擊Maven,選中要查看依賴關係圖的項目,點擊閃電圖標旁的雙箭頭圖標
按住Ctrl+鼠標滾輪縮放將依賴關係圖片放大
按Ctrl+F查找logback jar,從圖中可以看到同時包含logback-classic和spring-boot-starter-logging 日誌依賴
雙擊logback-classic,可以看到spring-boot-starter-logging中依賴了logback-classic
查找spring-boot-starter-logging
下圖可以看出spring-boot-starter中依賴了spring-boot-starter-logging
查看spring-boot-starter是被哪個jar依賴
排除spring-boot-starter-logging依賴
啓動項目成功