idea报错-LoggerFactory is not a Logback LoggerContext but Logback is on the classpath

博客内容讲述了在Spring Boot应用启动时遇到的SLF4J绑定错误和Servlet版本冲突问题。通过排除pom.xml文件中不必要的依赖,如slf4j-log4j12和servlet-api,解决了SLF4J的多个绑定问题。然而,这又引发了新的错误,最终通过调整排除策略,成功启动了应用。问题解决的关键在于正确管理项目依赖,避免类路径上的冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误提示

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from file:/C:/Users/yfbz/.m2/repository/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.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.atguigu.springclloud.PaymentMain8004.main(PaymentMain8004.java:17)
	... 5 more

参考资料

官网描述

http://www.slf4j.org/codes.html#multiple_bindings
无法加载类org.slf4j.impl.StaticLoggerBinder
当类无法加载到内存中时,将报告此警告消息。当在类路径上找不到适当的 SLF4J 绑定时,将发生这种情况。将一个(也是只有一个)的slf4j-nop.jar slf4j-简单.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或回溯经典.jar在类路径上应该解决问题。 org.slf4j.impl.StaticLoggerBinder

请注意,slf4j-api 版本 2.0.x 和以后使用ServiceLoader机制。后端(如注销 1.3)以及以后的目标 slf4j-api 2.x 时,不要与 一起发货。如果将目标为 slf4j-api 2.0.x 的日志记录后端,则需要slf4j-api-2…jar在类路径上。另请参阅相关的 faq条目。org.slf4j.impl.StaticLoggerBinder

官网提示

在类路径上找到多个绑定
SLF4J API 设计为一次只绑定一个基础日志记录框架。如果类路径上存在多个绑定,SLF4J 将发出警告,列出这些绑定的位置。

当类路径上有多个绑定可用时,选择一个且仅选择一个要使用的绑定,然后删除其他绑定。例如,如果在类路径上同时具有slf4j-简单-2.0.0-0-alpha0.jar和slf4j-nop-2.0.0-alpha0.jar,并且希望使用 nop(无操作)绑定,则从类路径中删除 slf4j-simple-2.0.0-0-0.jar。

SLF4J 在此警告中提供的位置列表通常提供足够的信息来识别依赖关系,以临时方式将不需要的 SLF4J 绑定拉入您的项目。在项目的 pom .xml,在声明肆无忌惮的依赖关系时排除此 SLF4J 绑定。例如,所有 cassandra 版本0.8.1 将log4j 和 slf4j-log4j12声明为编译时间依赖项。因此,当您在项目中包括 cassandra-all作为依赖项时,cassandra-all声明将导致 slf4j-log412 和 log4j.jar .jar作为依赖项拉扯。如果您不希望使用 log4j 作为 SLF4J 后端,可以指示 Maven 排除这两个工件,如下所示:

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

  </dependency>
</dependencies>

将如上代码复制进入pom.xml文件
问题解决

再次出现其他错误

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1220)

The following method did not exist:

    javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

The method's class, javax.servlet.ServletContext, is available from the following locations:

    jar:file:/C:/Users/yfbz/.m2/repository/org/mortbay/jetty/servlet-api/2.5-20081211/servlet-api-2.5-20081211.jar!/javax/servlet/ServletContext.class
    jar:file:/C:/Users/yfbz/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
    jar:file:/C:/Users/yfbz/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.29/tomcat-embed-core-9.0.29.jar!/javax/servlet/ServletContext.class

It was loaded from the following location:

    file:/C:/Users/yfbz/.m2/repository/org/mortbay/jetty/servlet-api/2.5-20081211/servlet-api-2.5-20081211.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext

重复引入,去掉重复的

观察

在这里插入图片描述

在这里插入图片描述

找到spring-boot-starter-web

1、
在这里插入图片描述/2、右键单击
在这里插入图片描述
修改pom文件,将不需要的的排除,

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>

但是依然报错
此时将最初查资料的官网给出的解决方案注释掉,运行正常

<!--        <dependency>-->
<!--            <groupId> org.apache.cassandra</groupId>-->
<!--            <artifactId>cassandra-all</artifactId>-->
<!--            <version>0.8.1</version>-->

<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.slf4j</groupId>-->
<!--                    <artifactId>slf4j-log4j12</artifactId>-->
<!--                </exclusion>-->
<!--                <exclusion>-->
<!--                    <groupId>log4j</groupId>-->
<!--                    <artifactId>log4j</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->

<!--        </dependency>-->

正常运行截图

在这里插入图片描述

总结

总的来讲解决最初的问题就是要去掉多余引入的包
但是在去掉的过程中确造成了其他的问题

最终不断根据提示信息,找到了该排除错误的位置

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>

在zookeeper客户端进行测试

在这里插入图片描述
微服务的支付模块成功入驻金zookeeper服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值