You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot注入JIRA 7.1.6的CustomFieldManager/ComponentAccessor报错解决

解决JIRA 7.1.6中无法注入CustomFieldManager/使用ComponentAccessor的问题

你遇到的核心问题其实是运行环境的不匹配——JIRA的核心组件(比如CustomFieldManager)是在Atlassian的OSGi容器中管理的,而普通的SpringBoot应用完全独立于这个容器,直接依赖JIRA的jar包并尝试注入/获取组件肯定会失败,因为这些bean根本不在你的Spring上下文里,ComponentAccessor也找不到JIRA的运行时环境。

下面给你一步步梳理解决思路:

1. 先确认你的项目类型:必须是JIRA插件项目,而非普通SpringBoot应用

JIRA插件不能用普通SpringBoot初始化方式创建,必须使用Atlassian SDK来构建——它会帮你处理OSGi的配置、依赖管理,以及和JIRA容器的交互。如果你的项目是普通SpringBoot结构,那得先重构为JIRA插件项目。

2. 修正你的POM配置(关键)

你的POM肯定没按JIRA插件的规范来配置,这里给你一个基础的正确模板,替换掉你现有的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yourcompany.jira</groupId>
    <artifactId>your-plugin-key</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>atlassian-plugin</packaging>

    <parent>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-parent</artifactId>
        <version>7.1.6</version>
        <relativePath />
    </parent>

    <properties>
        <jira.version>7.1.6</jira.version>
        <amps.version>6.3.14</amps.version> <!-- 对应JIRA 7.1.x的amps版本 -->
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- JIRA核心API,scope必须是provided,因为JIRA运行时会提供这些类 -->
        <dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-api</artifactId>
            <version>${jira.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-core</artifactId>
            <version>${jira.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Atlassian Spring Scanner,用于插件内的Spring组件扫描 -->
        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-annotation</artifactId>
            <version>1.2.13</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-runtime</artifactId>
            <version>1.2.13</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>maven-jira-plugin</artifactId>
                <version>${amps.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <productVersion>${jira.version}</productVersion>
                    <productDataVersion>${jira.version}</productDataVersion>
                    <!-- 启用Spring扫描,这样插件内的@Scanned注解会被识别 -->
                    <enableSpringScanner>true</enableSpringScanner>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

重点注意:

  • 必须继承JIRA的parent pom
  • JIRA相关依赖的scope是provided,绝对不能用compile(否则会和JIRA运行时的类冲突)
  • 引入Atlassian Spring Scanner依赖,用于插件内的Spring组件管理

3. 正确注入CustomFieldManager的两种方式

方式一:在Spring管理的组件中依赖注入(推荐)

在JIRA插件中,如果你要注入JIRA的核心组件,必须用@ComponentImport标记,告诉Spring这个bean来自OSGi容器,而非本插件的上下文:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import javax.inject.Inject;

// 用@Scanned标记这个类是插件内的Spring组件
@Scanned
public class YourJiraService {

    private final CustomFieldManager customFieldManager;

    // 构造方法注入,用@ComponentImport标记CustomFieldManager
    @Inject
    public YourJiraService(@ComponentImport CustomFieldManager customFieldManager) {
        this.customFieldManager = customFieldManager;
    }

    // 业务方法示例
    public void doSomethingWithCustomField() {
        // 现在可以正常使用customFieldManager了
        customFieldManager.getCustomFieldObjects();
    }
}

方式二:用ComponentAccessor获取(适合非Spring管理的类)

如果你的代码不在Spring组件中,可以用ComponentAccessor,但要确保代码是在JIRA插件的运行环境中执行的(比如插件的web请求、任务调度中):

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;

public class YourNonSpringClass {
    public void someMethod() {
        // 获取CustomFieldManager实例
        CustomFieldManager customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class);
        // 后续操作...
    }
}

⚠️ 注意:不要在插件启动的早期阶段(比如BundleActivator的start方法)使用ComponentAccessor,此时JIRA的核心组件可能还没初始化完成,会导致空指针或者获取失败。

4. 检查插件配置文件atlassian-plugin.xml

确保你的插件配置文件中没有禁用Spring扫描,并且正确声明了插件的基本信息,比如:

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="Your Company" url="https://yourcompany.com"/>
    </plugin-info>

    <!-- 如果不需要手动声明组件,启用Spring扫描后会自动识别@Scanned的类 -->
</atlassian-plugin>

5. 常见错误排查

  • 如果你之前是普通SpringBoot项目,直接改POM是不够的,必须用Atlassian SDK创建插件骨架(可以用atlas-create-jira-plugin命令生成)
  • 检查依赖的scope:JIRA的jar包必须是provided,否则会导致类加载冲突
  • 忘记加@ComponentImport:注入JIRA核心组件时必须加这个注解,否则Spring找不到对应的bean

内容的提问来源于stack exchange,提问作者du-it

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:27:47