zoukankan      html  css  js  c++  java
  • SLF4J: Class path contains multiple SLF4J bindings.警告解决

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/G:/repo/repo-mvn-qingmu/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:/G:/repo/repo-mvn-qingmu/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.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

    问题解决:
      当然问题我不敢确定一定是因为war 和jar 的原因,你们也可能不关心这个,我们只想知道如何解决这种问题而已。
      问题解决办法很简单,就是既然抛了jar包冲突 ,那我们就排除一个jar 包即可。关键是排除哪一个jar包 ,这里注意下了,如果你用的是logback 日志,一定要排除slf4j-log4j12 包,不要排除logback-classic 包。
      即找到pom.xml 文件,如果你们的开发工具,比如eclipse 和idea 都可以看引入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>
  • 相关阅读:
    《JAVA高并发编程详解》-Thread start方法的源码
    《JAVA高并发编程详解》-Thread对象的启动
    作为程序员,我建议你学会写作
    【灵异短篇】这个夜晚有点凉
    JAVA中for与while关于内存的细节问题
    通过本质看现象:关于Integer受内部初始化赋值范围限制而出现的有趣现象
    【设计模式】抽象工厂模式
    【设计模式】工厂模式
    【设计模式】单例模式
    【设计模式】基本介绍
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12312834.html
Copyright © 2011-2022 走看看