在IDEA运行中发现这种错误,java我用的是17,maven用的是3.8.8
一、第一次解决(失败版本)
1. 错误发生在应用程序启动阶段,当Spring Boot尝试初始化日志系统时
2. 具体是在LogbackLoggingSystem类中试图获取logger上下文时抛出的
3. 这表明项目中已经有了SLF4J API,但缺少具体实现
解决方案应该是添加logback-classic依赖,因为Spring Boot默认使用Logback作为日志实现。
修改加上了这个依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
结果就是又出现了这一堆的bug
D:\JAVA17\bin\java.exe -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA\IntelliJ IDEA 2022.2\lib\idea_rt.jar=53913:D:\IDEA\IntelliJ IDEA 2022.2\bin" -Dfile.encoding=UTF-8 -classpath E:\A-study\ideaProjects\spring-code\springboot-quickstart\target\classes;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-starter-web\3.4.0\spring-boot-starter-web-3.4.0.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-starter\3.4.0\spring-boot-starter-3.4.0.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-autoconfigure\3.4.0\spring-boot-autoconfigure-3.4.0.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-starter-logging\3.4.0\spring-boot-starter-logging-3.4.0.jar;D:\Z-maven\myRepository\org\apache\logging\log4j\log4j-to-slf4j\2.24.1\log4j-to-slf4j-2.24.1.jar;D:\Z-maven\myRepository\org\apache\logging\log4j\log4j-api\2.24.1\log4j-api-2.24.1.jar;D:\Z-maven\myRepository\org\slf4j\jul-to-slf4j\2.0.16\jul-to-slf4j-2.0.16.jar;D:\Z-maven\myRepository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;D:\Z-maven\myRepository\org\yaml\snakeyaml\2.3\snakeyaml-2.3.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-starter-json\3.4.0\spring-boot-starter-json-3.4.0.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\core\jackson-databind\2.18.1\jackson-databind-2.18.1.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\core\jackson-annotations\2.18.1\jackson-annotations-2.18.1.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\core\jackson-core\2.18.1\jackson-core-2.18.1.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.18.1\jackson-datatype-jdk8-2.18.1.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.18.1\jackson-datatype-jsr310-2.18.1.jar;D:\Z-maven\myRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.18.1\jackson-module-parameter-names-2.18.1.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot-starter-tomcat\3.4.0\spring-boot-starter-tomcat-3.4.0.jar;D:\Z-maven\myRepository\org\apache\tomcat\embed\tomcat-embed-core\10.1.33\tomcat-embed-core-10.1.33.jar;D:\Z-maven\myRepository\org\apache\tomcat\embed\tomcat-embed-el\10.1.33\tomcat-embed-el-10.1.33.jar;D:\Z-maven\myRepository\org\apache\tomcat\embed\tomcat-embed-websocket\10.1.33\tomcat-embed-websocket-10.1.33.jar;D:\Z-maven\myRepository\org\springframework\spring-webmvc\6.2.0\spring-webmvc-6.2.0.jar;D:\Z-maven\myRepository\org\springframework\spring-aop\6.2.0\spring-aop-6.2.0.jar;D:\Z-maven\myRepository\org\springframework\spring-expression\6.2.0\spring-expression-6.2.0.jar;D:\Z-maven\myRepository\org\springframework\spring-core\6.2.0\spring-core-6.2.0.jar;D:\Z-maven\myRepository\org\springframework\spring-jcl\6.2.0\spring-jcl-6.2.0.jar;D:\Z-maven\myRepository\org\springframework\boot\spring-boot\2.6.1\spring-boot-2.6.1.jar;D:\Z-maven\myRepository\org\springframework\spring-context\6.2.0\spring-context-6.2.0.jar;D:\Z-maven\myRepository\io\micrometer\micrometer-observation\1.14.1\micrometer-observation-1.14.1.jar;D:\Z-maven\myRepository\io\micrometer\micrometer-commons\1.14.1\micrometer-commons-1.14.1.jar;D:\Z-maven\myRepository\org\springframework\spring-web\5.3.13\spring-web-5.3.13.jar;D:\Z-maven\myRepository\org\springframework\spring-beans\6.2.0\spring-beans-6.2.0.jar;D:\Z-maven\myRepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\Z-maven\myRepository\ch\qos\logback\logback-core\1.5.12\logback-core-1.5.12.jar;D:\Z-maven\myRepository\org\slf4j\slf4j-api\2.0.16\slf4j-api-2.0.16.jar com.itheima.springbootquickstart.SpringbootQuickstartApplication
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J(W): Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J(W): Ignoring binding found at [jar:file:/D:/Z-maven/myRepository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J(W): See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.
二、第二次解决
看到这个新的错误信息,我发现问题比之前想象的要复杂...
分析错误信息:
1. SLF4J没有找到providers
2. 默认使用了no-operation (NOP) logger
3. 类路径中包含了针对slf4j-api 1.7.x或更早版本的绑定
4. logback-classic 1.2.11的绑定被忽略了
问题的根源在于版本不匹配:
1. Spring Boot 3.4.0使用的是更新的SLF4J 2.x版本
2. 而我们添加的logback-classic 1.2.11是针对SLF4J 1.7.x的
3. spring-boot-starter-logging 3.4.0已经包含了更新的日志依赖
解决方案应该是:
1. 移除手动添加的logback-classic依赖
2. 让Spring Boot通过starter来管理日志依赖
3. 确保所有Spring相关依赖版本一致
还注意到pom.xml中存在版本不一致的问题:
- spring-boot 2.6.1
- spring-web 5.3.13
这些也需要处理,因为父工程是3.4.0版本
方案
1. 移除了手动添加的版本不匹配的依赖:
- spring-boot 2.6.1
- spring-web 5.3.13
- logback-classic 1.2.11
2. 只保留了最基本必需的依赖:
- spring-boot-starter-web (包含了web开发所需的所有依赖)
- spring-boot-starter-test (用于测试)
3. 所有依赖版本都由spring-boot-starter-parent (3.4.0)统一管理
这样的依赖管理更加简洁且避免了版本冲突。