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的類=====================");
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM