Jira插件注入TemporaryAttachmentsMonitorLocator提示无匹配Bean报错
问题根因
TemporaryAttachmentsMonitorLocator注入失败的核心原因是该服务的暴露逻辑和你插件的OSGi依赖配置不匹配:
- 低于Jira 8.0的版本中,该类不属于Jira核心API导出的服务,由Jira原生导入插件(JIM)单独暴露,
@ComponentImport默认只会扫描Jira核心bundle导出的服务,无法识别其他插件暴露的服务 - 高版本Jira中如果出现该报错,通常是你本地的jira-api依赖版本和测试环境的Jira版本不一致,或是OSGi导入声明缺失
- 低版本atlassian-spring-scanner不支持跨插件的注解式服务导入
修复方案
方案1:适配低版本Jira(<8.0)
- 在pom.xml中添加JIM插件依赖,scope设为provided
<dependency> <groupId>com.atlassian.jira.plugins</groupId> <artifactId>jira-import-plugin</artifactId> <version>匹配你目标Jira版本的JIM版本号</version> <scope>provided</scope> </dependency>
- 在
atlassian-plugin.xml中显式声明服务导入,替代注解式导入
<component-import key="temporaryAttachmentsMonitorLocator" interface="com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator" />
- 移除构造方法中该参数的
@ComponentImport注解即可
方案2:适配高版本Jira(≥8.0)
- 检查pom.xml中jira-api的依赖版本是否和测试环境Jira版本完全一致,scope保持为provided
- 升级
atlassian-spring-scanner依赖到2.1.7及以上版本,兼容跨bundle的注解式服务导入 - 清理插件构建缓存,重新打包部署即可
方案3:动态获取服务(兼容所有版本)
如果不想处理版本兼容问题,可以直接通过OSGi上下文动态获取服务,无需提前声明导入:
public class TrelloSetupPage extends AbstractSetupPage { private final TemporaryAttachmentsMonitorLocator locator; public TrelloSetupPage(@ComponentImport UsageTrackingService usageTrackingService, @ComponentImport WebInterfaceManager webInterfaceManager, @ComponentImport PluginAccessor pluginAccessor, @ComponentImport EventPublisher eventPublisher, @ComponentImport OsgiContainerManager osgiContainerManager) { super(usageTrackingService, webInterfaceManager, pluginAccessor, eventPublisher); // 动态获取目标服务 BundleContext bundleContext = osgiContainerManager.getBundleContext(); ServiceReference<TemporaryAttachmentsMonitorLocator> serviceRef = bundleContext.getServiceReference(TemporaryAttachmentsMonitorLocator.class); this.locator = bundleContext.getService(serviceRef); } }
内容的提问来源于stack exchange,提问作者Fran b
相关产品推荐
相关产品推荐

