SpringBoot整合log4j2导入新的依赖出现jar冲突解决

1、问题复现:

之前在SpringBoot中配置整合了log4j2,今天在pom文件中,导入新的依赖(依赖如下)之后,

<dependency>
   <groupId>com.github.tobato</groupId>
   <artifactId>fastdfs-client</artifactId>
   <version>1.26.7</version>
</dependency>   

重启SpringBoot项目时,出现如下错误:

Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/00_Develop_Install/Maven/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar). If you are using WebLogic you will need to add ‘org.slf4j‘ to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory
    at org.springframework.util.Assert.instanceCheckFailed(Assert.java:696)
    at org.springframework.util.Assert.isInstanceOf(Assert.java:596)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:281)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:104)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:70)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:47)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.pactera.attd.AttdApplication.main(AttdApplication.java:12)

在网上查找相关资料,大部分都是说要 排除SpringBoot默认的logback依赖,但是这个问题,我在整合时,已经去除掉了,所以,很显然不是这个问题。后来,看来篇文章说是,pom文件依赖中的顺序问题,后来就调整了一下顺序,然后重启,报了以下错误(报错信息太多,这里只截取了主要部分)。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/00_Develop_Install/Maven/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/00_Develop_Install/Maven/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.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]
Logging system failed to initialize using configuration from ‘classpath:log4j2.xml‘...

2、问题解决

仔细查看了这次报错的信息,大致意思是说,日志系统出现了多jar包冲突,由于我使用的是log4j2,结果显示还有一个logback的jar包,于是顺着这个问题,查看了一下项目所引jar包,发现了多了以下红框中所展示的jar包,将所引入坐标注释掉,此jar包则没有了,如下图所示。

引入坐标后:

注释坐标后:

这下,很明显就知道了原因,也就是说,自己新导入的pom坐标,并未将此jar包排除掉,导致出现了多jar包,既然这样,看来跟新引入的pom坐标的顺序也没关系了,只需要将原先的logback-classic排除掉即可,于是,就在引入的pom坐标下添加了如下排除的依赖。

<exclusions>
     <exclusion>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
     </exclusion>
</exclusions>

前后依赖对比,如图。

解决前:

 解决后:

注意:groupId和artifactId一定要写正确,并对应的依赖里面。

然后,重新build项目,发现冲突的jar包,没有了,之后重启SpringBootApplication,项目正常启动,问题解决。

原文地址:https://www.cnblogs.com/cndarren/p/12054224.html

时间: 2024-08-06 02:38:48

SpringBoot整合log4j2导入新的依赖出现jar冲突解决的相关文章

springboot整合log4j2日志框架

springboot依赖中默认引入spring-boot-starter-logging,若要使用log4j2日志框架,则需要先将spring-boot-starter-logging排除. 方法: 在依赖关系图中找到spring-boot-starter-logging,单击右键选择exclude将其排除出去. pom文件中引入: <dependency> <groupId>org.springframework.boot</groupId> <artifact

springboot整合log4j2

1,找到第一个启动类,排除logging 1 <exclusions><!-- 去掉springboot默认配置 --> 2 <exclusion> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-logging</artifactId> 5 </exclusion> 6 </exclusion

SpringBoot整合MYBATIS,多数据源,事务,支持JAVA -JAR 启动.

用了一段时间SpringBoot,之前配置MYBATIS ,在打包WAR 放到tomcat下正常,但是WAR已经过时了,现在流行直接打包JAR 丢到DOCKER 里,无奈JAR 启动的时候MAPPER 扫描有问题,只能说之前整合MYBATIS 的方式不对. 这次的整合应该是不存在加载顺序引起的问题,使用了一段时间,妥妥的,记录下来 pom.xml <parent> <groupId>org.springframework.boot</groupId> <artif

JAVA导入新项目代码中有乱码的解决办法

1.在项目上右键properties,选择Resource,更改Text file encoding   设置(bgk或者utf-8  等...) 2.在IDE(Eclipse),选择Window--->preferences---->General---->Workspace----找到Text file encoding  设置 编码格式 3.(我使用的)在IDE(Eclipse),选择Window--->preferences---->General---->Co

spring boot 1.5.4 整合log4j2(十一)

上一篇:spring boot 1.5.4 定时任务和异步调用(十) Spring Boot整合log4j2 spring boot整合log4j2项目spring-boot-jsp源码: https://git.oschina.net/wyait/springboot1.5.4.git 1.1  log4j2概要 对于我们开发人员来说,日志记录往往不被重视.在生产环境中,日志是查找问题来源的重要依据.日志可记录程序运行时产生的错误信息.状态信息.调试信息和执行时间信息等多种多样的信息.可以在程

springboot整合drui、mybatis、pagehelper

springboot配置数据连接池druid Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池. 在功能.性能.扩展性方面,都超过其他数据库连接池,同时加入了日志监控, 可以很好的监控DB池连接和SQL的执行情况 配置pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactI

springboot整合数据库

springboot整合数据库 整合jdbc 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId&g

springboot学习笔记-5 springboot整合shiro

http://www.cnblogs.com/hlhdidi/p/6376457.html 亲自验证,该帖真实有效 shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springboot结合springmvc,mybatis,整合shiro完成对于用户登录的判定和权限的验证. 1.准备数据库表结构 这里主要涉及到五张表:

springboot 整合jdbcTemplate

springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency&g