activiti监听器无法实例化(SpringBoot)

SpringBoot项目中,activiti某个流程节点配置了任务监听器,代码如下

public class ApplyListener implements TaskListener, ExecutionListener {
    private static final long serialVersionUID = -6000293034409953144L;

    @Autowired
    private TaskService taskService;
    @Override
    public void notify(DelegateTask delegateTask) {


        taskService.setAssignee(delegateTask.getId(),"zhangsan");
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {

    }
}

执行任务提交报错:Exception while invoking TaskListener: null

个人理解,上述过程中MyTaskListenre虽然存在于Spring容器中,但未被Spring容器所管理,所以@Autowired失效

 

解决方案:

写一个工具类,手动获取Bean


 @Component
public class SpringUtil implements ApplicationContextAware {
 
   /**
    * 当前IOC
    *
    */
   private static ApplicationContext applicationContext;
    
    /**
     * 设置applicationContext
     */
   @Override
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
   }
 
   public static T T getObject(ClassT clazz){
       return applicationContext.getBean(clazz);
   }
}

修改 监听器类代码

@Component
@Slf4j
public class ApplyListener implements TaskListener, ExecutionListener {
    private static final long serialVersionUID = -6000293034409953144L;

 
    @Override
    public void notify(DelegateTask delegateTask) {

        TaskService taskService = SpringUtil.getObject(TaskService.class);
        taskService.setAssignee(delegateTask.getId(),"zhangsan");
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {

    }
}

 

最新回复(0)
/jishuQes_2FbBUdy9YVuZYjlfFDciqWMVL6AXBumhzyzA2Xzo0_3D4674127
8 简首页