springboot log4j2 報(bào)錯(cuò)SLF4J: Class path contains multiple SLF4J bindings,maven依賴重復(fù)沖突的解決辦法
springboot 使用log4j保存日志,網(wǎng)上隨便找了篇博文參考,配置依賴照寫,啟動(dòng)結(jié)果報(bào)錯(cuò): Class path contains multiple SLF4J bindings
具體報(bào)錯(cuò)如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/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:108)
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:121)
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)
報(bào)錯(cuò)字面意思 有多個(gè) SLF4J 綁定,log4j-slf4j-impl-2.11.2.jar 和 logback-classic-1.2.3.jar里面重復(fù)綁定SLF4J ,再看了下那博文,有這么一段,如項(xiàng)目中有導(dǎo)入spring-boot-starter-web依賴包記得去掉spring自帶的日志依賴spring-boot-starter-logging,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
按上面說(shuō)法是 spring-boot-starter-web 自帶日志依賴,那既然exclusion排除了日志依賴咋還有問(wèn)題!
實(shí)際是 spring-boot-starter 自帶日志依賴 而不是 spring-boot-starter-web,下面才是正確的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
如何快速正確的解決類似重復(fù)依賴的問(wèn)題。
1 右鍵pom.xml -> Maven -> Show Dependencies

2 搜索重復(fù)的jar包被哪些上層依賴
比如 這里要搜索的是 logback-classic-1.2.3.jar 在哪里被依賴
在上面打開的依賴關(guān)系圖中 crtl + f 打開搜索,然后輸入搜索內(nèi)容 logback-classic,如下:
點(diǎn)擊找到的logback-classic ,會(huì)看到如下依賴關(guān)系:

3 找到了需要排除的jar被誰(shuí)用到
那就在pom.xml中用排除掉,
如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
最后
其他類似的maven 重復(fù)依賴問(wèn)題,可以按上面方法嘗試去解決!