springboot 使用log4j保存日志,网上随便找了篇博文参考,配置依赖照写,启动结果报错: Class path contains multiple SLF4J bindings
具体报错如下:

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)

报错字面意思 有多个 SLF4J 绑定,log4j-slf4j-impl-2.11.2.jar 和 logback-classic-1.2.3.jar里面重复绑定SLF4J ,再看了下那博文,有这么一段

如项目中有导入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>

按上面说法是 spring-boot-starter-web 自带日志依赖,那既然exclusion排除了日志依赖咋还有问题!

实际是这个博主弄错了 是 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>

当然 本文不是为了吐槽这位粗心的博主,而是要介绍如何快速正确的解决类似重复依赖的问题。

  • 1 右键pom.xml -> Maven -> Show Dependencies
    在这里插入图片描述
    在这里插入图片描述
  • 2 搜索重复的jar包被哪些上层依赖,比如 这里要搜索的是 logback-classic-1.2.3.jar 在哪里被依赖
    在上面打开的依赖关系图中 crtl + f 打开搜索,然后输入搜索内容 logback-classic,如下:
    在这里插入图片描述
    点击找到的logback-classic ,会看到如下依赖关系:
    在这里插入图片描述
  • 3 找到了需要排除的jar被谁用到,那就在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 重复依赖问题,可以按上面方法尝试去解决!

更多推荐