activiti执行监听器executionListener Expression用法
解决方法:
使用Expression的方式来为开始节点设置监听器处理类,方法表达式Expression内容:${myExecutionListener2.notify2(execution,authenticatedUserId)},如下所示
<startEvent id="_2" name="StartEvent">
<extensionElements>
<activiti:executionListener event="start" expression="${myExecutionListener2.notify2(execution,authenticatedUserId)}"/>
</extensionElements>
</startEvent>
1.执行监听器executionListener处理类代码,spring会自动注入到activiti表达式${myExecutionListener2.notify2(execution,authenticatedUserId)}中的myExecutionListener2:
@Service
public class MyExecutionListener2{
private static final long serialVersionUID = 8513750196548027535L;
public void notify2(DelegateExecution execution,String startUserId) throws Exception {
System.out.println("开始节点" );
}
}
2.启动流程
try {
identityService.setAuthenticatedUserId("123456");//设置启动人id为123456
Map variableMap = new HashMap();
ProcessInstance pi = runtimeService.startProcessInstanceByKey("myProcess", variableMap);
} finally {
identityService.setAuthenticatedUserId(null);
}
3.结果:
启动时identityService.setAuthenticatedUserId("123456");就是把启动人用户id:123456存放到activiti默认变量authenticatedUserId中,也就是说authenticatedUserId的值就是你设置的123
456,这个变量在任何${}表达使用都可以使用。就如上面${myExecutionListener2.notify2(execution,authenticatedUserId)},我就把启动人id作为参数传到MyExecutionListener2的notify2方法中,所以MyExecutionListener2的notify2的参数startUserId值就是123456。
表达式中参数execution名字在执行监听器中是固定的,就是DelegateExecution对象,改别的名称就报错。如果是任务监听器,参数task代表DelegateTask对象,也是固定的。