SpringBoot2.7.5启动报错:NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
引入spring-boot-starter-web依赖,使用其自带的spring-boot-starter-logging框架,在yaml配置文件中指定logback-spring.xml作为日志配置,启动项目时出现NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder错误,直接在pom中导入slf4j 2.0.3依赖也无法解决问题。
相关配置
YAML配置
logging: level: root: info com.felix.flink.tutorial.api: debug config: classpath:logback-spring.xml
Maven依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.felix</groupId> <artifactId>flink-tutorial-component</artifactId> <version>${revision}</version> </dependency> </dependencies>
异常信息
23:45:33.009 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@7abaedae Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:293) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:238) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:79) at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:56) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:56) at org.springframework.boot.SpringApplication.run(SpringApplication.java:299) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.felix.flink.tutorial.api.FlinkTutorialApiApplication.main(FlinkTutorialApiApplication.java:15) ... 5 more Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 22 more Process finished with exit code 0
检查依赖冲突,确保logback-classic存在
spring-boot-starter-logging默认包含logback-classic(它实现了SLF4J的StaticLoggerBinder),如果这个依赖被间接排除就会触发错误。执行mvn dependency:tree查看依赖树,确认是否有依赖排除了logback-classic。若发现冲突,手动在spring-boot-starter-web依赖中声明正确的日志依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 排除冲突的日志框架依赖,比如log4j2 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 手动引入Spring Boot管理的日志依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>移除手动添加的SLF4J依赖,对齐版本
SLF4J 2.x和Spring Boot 2.x存在兼容性问题(Spring Boot 2.x默认依赖SLF4J 1.x),手动引入SLF4J 2.0.3会打破版本对齐——SLF4J 2.x中已移除StaticLoggerBinder类,改用服务发现机制。直接移除手动添加的SLF4J依赖,让Spring Boot的依赖管理自动控制版本。排查devtools类加载问题
异常中出现RestartClassLoader,可能是devtools的类加载器未正确加载logback相关类。可以尝试:- 临时移除
spring-boot-devtools依赖,测试项目是否能正常启动 - 在
logback-spring.xml中添加classLoader="com.sun.tools.attach.VirtualMachine",修复类加载器识别问题
- 临时移除
验证logback配置文件路径
确保logback-spring.xml确实在src/main/resources目录下,路径无拼写错误。若路径正确仍报错,可尝试将配置文件名改为logback.xml,规避Spring Boot的配置加载优先级问题。
内容的提问来源于stack exchange,提问作者felixbupt

