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

Jira Java插件报错:ComponentAccessor未初始化,求解决方案

问题:ComponentAccessor未初始化导致Jira插件代码执行失败

执行Jira Java插件代码时遇到如下错误:

Exception in thread "main" java.lang.IllegalStateException: ComponentAccessor has not been initialised.
This is not expected to occur on a production system...

已查阅相关帖子但无法解决,代码如下:

package com.atlassian.ccft.impl;

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

public class mainCCFT {

    public static void main(String[] args) {
        String IssueReference = "CAP-3";
        String customFieldId = "CCFT Forecast Finish";
//      float UpdatedForecastFinish = (float) 41.6;
        
        // Instantiate the issue
//      ComponentAccessor issue = new ComponentAccessor();
        
        // Get the issue
        MutableIssue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(IssueReference);
        
        // Get the custom field value
        Object forecastFinish = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customFieldId));
        
        System.out.println(forecastFinish);
    }
}

预期输出为该自定义字段的值,已查阅相关社区讨论但未解决问题。


解决方法
  1. 核心原因:ComponentAccessor是Jira的Spring容器管理的工具类,只有在Jira应用启动、容器完成初始化后才能正常调用。直接通过main方法运行代码时,Jira的完整运行环境并未启动,自然无法初始化ComponentAccessor。

  2. 正确执行路径:

    • 将代码集成到Jira插件的功能模块中,比如作为工作流函数、自定义字段逻辑、后台任务或Servlet接口,部署到Jira实例中运行,此时Jira容器会自动完成所有组件的初始化。
    • 如果需要本地测试,使用Atlassian SDK提供的测试框架(如Junit配合JiraMockery),在测试类中初始化Jira模拟环境,示例代码如下:
      import com.atlassian.jira.component.ComponentAccessor;
      import com.atlassian.jira.issue.MutableIssue;
      import com.atlassian.jira.junit.rules.AvailableInContainer;
      import com.atlassian.jira.junit.rules.JiraMockery;
      import org.junit.Rule;
      import org.junit.Test;
      import static org.jmock.Expectations.*;
      
      public class CCFTTest {
          @Rule
          public JiraMockery mockery = new JiraMockery();
      
          @AvailableInContainer
          private final IssueManager issueManager = mockery.mock(IssueManager.class);
      
          @Test
          public void testCustomFieldValue() {
              // 模拟IssueManager返回指定问题
              MutableIssue mockIssue = mockery.mock(MutableIssue.class);
              mockery.checking(new Expectations() {{
                  oneOf(issueManager).getIssueByCurrentKey("CAP-3");
                  will(returnValue(mockIssue));
              }});
      
              // 后续逻辑测试
              Object forecastFinish = mockIssue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12345"));
              System.out.println(forecastFinish);
          }
      }
      
  3. 代码额外问题修正:getCustomFieldObject()方法需要传入自定义字段的系统ID(格式为customfield_XXXXXX),而非字段名称"CCFT Forecast Finish"。即使解决了初始化问题,用名称也无法获取到目标字段,你可以在Jira后台的自定义字段配置页面找到对应字段的ID。

内容的提问来源于stack exchange,提问作者Byron G.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 19:28:13