使用Sprongboot引入pom文件,发现log4j2冲突,pom文件如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<optional>true</optional>
</dependency>
启动报错:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/maven/mymaven/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/maven/mymaven/org/apache/logging/log4j/log4j-slf4j-impl/2.17.2/log4j-slf4j-impl-2.17.2.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]
使用idea自带图表依赖项工具排查问题:
如图所示:
1. spring-boot-starter-logging 被 spring-boot-starter 引用,而 spring-boot-starter 被 spring-boot-starter-web 引用
2. spring-boot-starter-logging 同时又被 spring-boot-starter-log4j2 引用,这是导致冲突的原因。
解决方案:
1. pom文件注释 spring-boot-starter-log4j2的引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-log4j2</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
2. 屏蔽 spring-boot-starter-web下的 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<optional>true</optional>
</dependency>
根据不同使用场景,选择合理方案。
扩展:使用idea扩展工具排查
安装成功后,重启idea,打开pom文件,下面会有一个Dependency Analyzer标签,点击该标签,输入查询冲突包关键字,选择如图按钮:
这样依赖关系一目了然,再去屏蔽相关依赖。