springboot2.7.5启动错误 Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

fel*_*upt 2 java logging logback slf4j spring-boot

我引入了spring-boot-starter-web,使用了自带的spring-boot-starter-logging框架,在yaml中指定了配置文件,启动报错yaml:

logging:
  level:
    root: info
    com.felix.flink.tutorial.api: debug
  config: classpath:logback-spring.xml
Run Code Online (Sandbox Code Playgroud)

行家:

<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>
Run Code Online (Sandbox Code Playgroud)

例外:

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

Run Code Online (Sandbox Code Playgroud)

我在 pom 中直接导入了 slf4j 2.0.3,但它不起作用

Rob*_*oor 5

SLF4J 极大地改变了 1.x 和 2.x 版本之间的实现方式。在 1.x 中,绑定类需要提供一个名为org.slf4j.impl.StaticLoggerBinder- 缺少的类。在 2.x 中它使用了该ServiceLoader机制。

Spring Boot 目前仍然使用 SLF4J 1.7.36,通过spring-boot-starter-web-> spring-boot-starter-> spring-boot-starter-logging。后者依赖于一些 SLF4J 桥接器,而logback-classic后者又依赖于 SLF4J 1.7.32。我认为 1.7.36 “胜过”1.7.32。

除非您的其他依赖项之一具有对 SLF4J 2.x 的传递依赖项,否则一切都应该正常工作。如果这样做,那么您就会混合使用 SLF4J 1.x 和 2.x,而这根本行不通。将 2.x 依赖项替换为 1.x 依赖项,应该没问题(除非您使用 2.x 中添加的 Fluent API)。