flowable任务监听器和java服务任务依赖注入问题

记录最近使用flowable任务监听器和java服务任务相关的一个问题

任务监听器

TaskListener 主要是监听usertask的情况,监听事件event有4种:
create 创建
assignment 分配人
complete 完成
delete 删除

xml:

<userTask id="USERTASK" name="USERTASK" >
  <extensionElements>
    <activiti:taskListener event="complete" class="com.github.flowable.delegate.MyListener"/>
  </extensionElements>
</userTask>

代码:

public class MyListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {

        System.out.println("===========执行监听器============");
    }

}

这里会有一个问题,当有spring依赖注入时,会获取不到,看了一下应该是,流程引擎启动时,依赖注入还未初始化完成,因为是groovy和java是在工程中混着用的,可能会存在这个问题

依赖注入

这里通过另外一种方式解决,通过ApplicationContextAware接口的方式获取ApplicationContext对象实例

@Component
public class MyListener implements TaskListener, ApplicationContextAware {

    private static  ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        applicationContext = arg0;
    }

    @Override
    public void notify(DelegateTask delegateTask) {

        String processInsId = delegateTask.getProcessInstanceId();
        MyService myService = (MyService) applicationContext.getBean("myService");

        // TODO 执行service方法
        
        System.out.println("==========执行监听器======");
    }

}

java服务任务

同样java服务任务也存在这个问题
xml:

    <serviceTask id="classservicetask" name="class方式" flowable:class="com.github.flowable.delegate.ClassImplementsJavaDelegate">
    </serviceTask>

代码:

@Component
public class ClassImplementsJavaDelegate implements JavaDelegate, ApplicationContextAware {

	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		applicationContext = arg0;
	}
	
	public void execute(DelegateExecution execution) {

		String processInsId = execution.getProcessInstanceId();
		MyService myService = (MyService) applicationContext.getBean("myService");
		System.out.println("=====================指定实现了JavaDelegate的类=====================");
	}
}

猜你喜欢

转载自www.cnblogs.com/DF-Kyun/p/12669095.html