在activit项目中,有时需要重写节点的behaviour,但如果将代码反编译,会为后续升级,及项目打包带为不方便。
其实 acitivit已经提供了扩展点重写节点行为。
重写办法如下:
- activiti在初始化processEngineConfiguration时,设置行为工厂
@Bean(name = "processEngineConfiguration")
public ProcessEngineConfigurationImpl processEngineConfiguration() {
SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
processEngineConfiguration.setDataSource(druidDataSource());
processEngineConfiguration.setTransactionManager(annotationDrivenTransactionManager());
processEngineConfiguration.setProcessEngineName(ProcessEngines.NAME_DEFAULT);
//设置全局事件监听
processEngineConfiguration.setTypedEventListeners(this.getGlobalActivitiEventListener());
//重写节点行为
processEngineConfiguration.setActivityBehaviorFactory(new CdpActivityBehaviorFactory());
//设置主键生成
processEngineConfiguration.setIdGenerator(new ActivitiIdGenerator());
//设置函数
processEngineConfiguration.setBeans(FunctionFactory.getFunctionBeans());
return processEngineConfiguration;
}
其中关键代码为
//重写节点行为
processEngineConfiguration.setActivityBehaviorFactory(new CdpActivityBehaviorFactory());
2.类CdpActivityBehaviorFactory是节点行为工厂,设置了所有节点的行为实现类
@Component
public class CdpActivityBehaviorFactory extends DefaultActivityBehaviorFactory {
public CdpUserTaskActivityBehavior createUserTaskActivityBehavior(UserTask userTask, TaskDefinition taskDefinition) {
return new CdpUserTaskActivityBehavior(userTask.getId(), taskDefinition);
}
上述代码中CdpUserTaskActivityBehavior是这边重写的节点行为类
3.类CdpUserTaskActivityBehavior为节点行为,可以继承原来的UserTaskActivityBehavior,覆盖需要重写的方法即可
public class CdpUserTaskActivityBehavior extends UserTaskActivityBehavior{
//重写需要改的方法
}
最后强烈提醒一下,节点userTask需要重写的行为有三个,因为有单实例,串行多实例,并行多实例三种,分别为UserTaskActivityBehavior,ParallelMultiInstanceBehavior,SequentialMultiInstanceBehavior。