springboot 关于 Class path contains multiple SLF4J bindings.警告的解决

springboot 关于 Class path contains multiple SLF4J bindings.警告的解决

  有一次配置好springboot项目启动后,忽然发现有下边的警告:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/org/slf4j/slf4j-log4j12/1.7.22/slf4j-log4j12-1.7.22.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]
原因分析:

  上边的大概意思是说logback-classic 包和slf4j-log4j12 包,关于org/slf4j/impl/StaticLoggerBinder.class 这个类发生了冲突。
  发生这个错误的原因,首先logback 日志的开发者和log4j 的开发者据说是一波人,而springboot 默认日志是,较新的logback 日志。但是在以前流行的日志却是log4j ,而且很多的第三方工具都含有log4j 得引入。
  而我们在项目开发中,难免会引入各种各样的工具包,所以,基本上springboot 项目,如果不注意,肯定会出现这种冲突的。

问题隐患:

  当然最关心的是它是否有隐患,如果你在开发工具中运行,对,没毛病,一般会正常启动。
  经过我使用情况中的观察,貌似springboot 配置成tomcat运行 ,即修改成war 包之后,一般这个警告没有什么影响;但是如果是传统的jar 包,尽管你在开发工具中能正常运行,也可能在打完包之后不能运行。

问题出现:

  因为我们是分布式项目开发,服务层作为一个jar 运行,而接口调用和前端页面,作为一个war 一起运行,也就是说我们即有war ,又有jar ,项目部署的时候,需要打完包之后运行才可以。
  在打完包之后,war 包能正常运行,jar 包不能正常运行,报了如下错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

Caused by: java.lang.ExceptionInInitializerError
        at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
        at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45
)
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)

        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:155)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:132)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
        at org.springframework.boot.SpringApplication.<clinit>(SpringApplication
.java:190)
        at spingboot.study.SpringbootStudyApplication.main(SpringbootStudyApplic
ation.java:14)
        ... 8 more
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar A
ND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See
 also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
        at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54
)
        ... 19 more
问题解决:

  当然问题我不敢确定一定是因为warjar 的原因,你们也可能不关心这个,我们只想知道如何解决这种问题而已。
  问题解决办法很简单,就是既然抛了jar包冲突 ,那我们就排除一个jar 包即可。关键是排除哪一个jar包 ,这里注意下了,如果你用的是logback 日志,一定要排除slf4j-log4j12 包,不要排除logback-classic 包。
  即找到pom.xml 文件,如果你们的开发工具,比如eclipseidea 都可以看引入jar 包的联系,比如idea可以这样看到你的依赖结构:
这里写图片描述
  点击后,弹出下边的这样的结构:

这里写图片描述
  通过上图中,你可以看到zookeeper 包中默认引入了slf4j-log4j12包,除此之外,还有我们springboot 一定引入的spring-boot-starter-web 包,它里边也有这个slf4j-log4j12 引入。
  我们只要在pom.xml 里排除这个即可。
如下:

<dependency>
     <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!--排除这个slf4j-log4j12-->
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
</dependency>

  下边是我们项目引入的第三方的工具包中:

 <dependency>
    <groupId>org.apache.zookeeper</groupId>
     <artifactId>zookeeper</artifactId>
     <version>3.4.8</version>
     <!--排除这个slf4j-log4j12-->
     <exclusions>
         <exclusion>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
         </exclusion>
     </exclusions>
 </dependency>
  • 147
    点赞
  • 309
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 66
    评论
引用\[1\]和\[2\]中提到了相同的错误信息,即"Class path contains multiple SLF4J bindings"。这个错误通常是由于项目中存在多个SLF4J实现类的绑定引起的。解决这个问题的方法是删除其中一个实现类的依赖。 具体的解决方案如下: 1. 首先,确定你想要使用的SLF4J实现类是哪个。在引用\[1\]中,想要使用的是slf4j-log4j12-1.6.4.jar,而在引用\[2\]中,想要使用的是logback-classic-1.2.3.jar。 2. 打开你的项目的依赖配置文件(比如pom.xml或build.gradle),找到与SLF4J相关的依赖项。 3. 删除其中一个实现类的依赖项。如果你想要使用slf4j-log4j12,那么删除logback-classic的依赖项;如果你想要使用logback-classic,那么删除log4j-slf4j-impl的依赖项。 4. 保存并重新构建你的项目。 这样,你就解决了"Class path contains multiple SLF4J bindings"的问题。请注意,如果你删除了一个实现类的依赖项,你可能需要相应地调整你的日志配置文件,以确保日志记录正常工作。 #### 引用[.reference_title] - *1* [遇到Class path contains multiple SLF4J bindings.该如何解决](https://blog.csdn.net/chuyouyinghe/article/details/79134894)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SLF4J: Class path contains multiple SLF4J bindings.](https://blog.csdn.net/qq_39323945/article/details/125765205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Class path contains multiple SLF4J bindings.问题原因及解决方案](https://blog.csdn.net/hsuehgw/article/details/128057054)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 66
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茁壮成长的凌大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值