Spring项目添加Thymeleaf后出现SLF4J StaticLoggerBinder加载失败求助
问题描述
为Spring项目添加Bootstrap时,在HTML标签中引入Thymeleaf命名空间后,出现了Failed to load class 'org.slf4j.impl.StaticLoggerBinder'错误。
HTML代码
<!DOCTYPE html> <html lang="en" xmlns:th="https://thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Welcome to my Application</title> </head> <body> <h1>Welcome to my Application</h1> </body> </html>
完整错误日志
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119) at java.base/java.lang.reflect.Method.invoke(Method.java:578) 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.helpers.NOPLoggerFactory loaded from file:/C:/Users/Ariciu/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.helpers.NOPLoggerFactory at org.springframework.util.Assert.instanceCheckFailed(Assert.java:713) at org.springframework.util.Assert.isInstanceOf(Assert.java:632) at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:369) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:121) 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.multicastInitialEvent(EventPublishingRunListener.java:136) at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:75) at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:54) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:54) at org.springframework.boot.SpringApplication.run(SpringApplication.java:300) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) at com.vladlaudat.java_crud_app.JavaCrudAppApplication.main(JavaCrudAppApplication.java:10) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ... 2 more
解决方案
这个错误和Thymeleaf命名空间、Bootstrap无关,是SLF4J日志框架的依赖冲突导致的:
- 日志明确提示:类路径上同时存在Logback和SLF4J的NOP(无操作)日志实现,Spring Boot默认想用Logback,但SLF4J找不到对应的绑定类,最终加载了NOP实现,引发冲突。
解决步骤:
- 统一SLF4J版本:
- 移除手动指定的
slf4j-api依赖(比如你这里的1.7.7版本),让Spring Boot的父依赖自动管理日志组件版本,避免版本不匹配问题。
- 移除手动指定的
- 排除冲突依赖:
- 检查项目依赖树,找到间接引入SLF4J其他实现(如
slf4j-nop、slf4j-simple)的依赖,在Maven中通过<exclusions>排除:<dependency> <!-- 替换为你项目中引入冲突的依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> </exclusion> </exclusions> </dependency>
- 检查项目依赖树,找到间接引入SLF4J其他实现(如
- 确保Logback依赖正常:
- Spring Boot的
spring-boot-starter-web或spring-boot-starter默认包含spring-boot-starter-logging(依赖Logback),如果项目中移除了这个starter,需手动添加:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
- Spring Boot的
内容的提问来源于stack exchange,提问作者VladLaudat
相关产品推荐
相关产品推荐

