SpringBoot连接AISSTREAM.IO时LoggerFactory冲突启动异常
SLF4J日志框架冲突解决:Logback与slf4j-simple共存问题
问题描述
将连接AISSTREAM.IO的代码作为Spring应用组件运行时,启动抛出java.lang.IllegalArgumentException,核心原因是类路径同时存在Logback和slf4j-simple两种SLF4J实现,导致Spring Boot默认的Logback日志系统无法正常初始化。即使排除了所有spring-boot-starter中的Logback,问题仍存在,说明slf4j-simple是由其他第三方依赖(大概率是AISSTREAM.IO的客户端依赖)引入的。
完整报错栈:
Exception in thread "main" 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.SimpleLoggerFactory loaded from file:/C:/Users/Asus/.m2/repository/org/slf4j/slf4j-simple/1.6.1/slf4j-simple-1.6.1.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.SimpleLoggerFactory at org.springframework.util.Assert.instanceCheckFailed(Assert.java:592) at org.springframework.util.Assert.isInstanceOf(Assert.java:511) at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:396) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:124) 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:178) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:171) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:149) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:137) 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:326) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) at com.demoandy.DemoandyApplication.main(DemoandyApplication.java:10) Disconnected from the target VM, address: '127.0.0.1:54575', transport: 'socket'
解决方案
1. 定位slf4j-simple的依赖来源
执行依赖树分析命令,找到引入slf4j-simple的第三方依赖:
- Maven:
mvn dependency:tree - Gradle:
./gradlew dependencies
在输出结果中搜索slf4j-simple,找到对应的父依赖(通常是AISSTREAM.IO的客户端依赖)。
2. 排除slf4j-simple依赖
在项目的依赖配置中,针对找到的父依赖添加排除规则:
Maven配置示例
<dependency> <groupId>【AISSTREAM客户端的groupId】</groupId> <artifactId>【AISSTREAM客户端的artifactId】</artifactId> <version>【对应版本号】</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </exclusion> </exclusions> </dependency>
Gradle配置示例
implementation('【AISSTREAM客户端的groupId】:【AISSTREAM客户端的artifactId】:【对应版本号】') { exclude group: 'org.slf4j', module: 'slf4j-simple' }
3. 验证依赖清理结果
重新执行依赖树命令,确认slf4j-simple已从类路径中移除,仅保留Spring Boot默认的Logback相关依赖。
4. 可选:强制指定Logback依赖(若上述步骤无效)
如果排除后仍存在冲突,可以显式声明Logback依赖,确保其优先级:
Maven配置
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>【与Spring Boot版本匹配的版本】</version> </dependency>
Gradle配置
implementation 'ch.qos.logback:logback-classic:【与Spring Boot版本匹配的版本】'
内容的提问来源于stack exchange,提问作者Manas Deshpande
相关产品推荐
相关产品推荐

