升级SLF4J至2.x后OSGi Bundle需osgi.serviceloader.processor扩展器问题
解决SLF4J 2.x在OSGi环境下的依赖解析失败问题
SLF4J 2.x版本改用Java ServiceLoader机制实现日志服务发现,在OSGi环境中需要osgi.serviceloader.processor扩展器来处理服务提供者的注册逻辑,这就是依赖解析失败的核心原因——当前环境缺少提供该扩展器的Bundle。
解决步骤
添加Apache Felix ServiceLoader Processor依赖
在你的build.gradle中补充以下运行时依赖,该Bundle提供了所需的osgi.serviceloader.processor扩展器:dependencies { compileOnly 'org.osgi:osgi.annotation:7.0.0' compileOnly 'org.osgi:org.osgi.service.component.annotations:1.3.0' implementation 'org.slf4j:slf4j-api:2.0.11' runtimeOnly 'org.slf4j:slf4j-simple:2.0.11' // 添加ServiceLoader Processor依赖 runtimeOnly 'org.apache.felix:org.apache.felix.serviceloader.processor:1.1.2' }重新构建与部署
执行构建命令重新生成Bundle,部署到OSGi容器后,依赖解析即可通过。该扩展器会自动处理SLF4J 2.x的ServiceLoader服务注册逻辑,让slf4j-simple能够正常绑定到slf4j-api。
补充说明
- 如果你使用的是基于bnd的构建工具(从项目名称
bnd-experiments判断大概率是),无需额外配置元数据,bnd会自动识别并处理相关依赖约束。 - 该依赖仅需运行时提供,无需编译时引入,因此使用
runtimeOnly配置即可。
内容的提问来源于stack exchange,提问作者Jmini
相关产品推荐
相关产品推荐

