一、前言
Spring Boot基于“習(xí)慣優(yōu)于配置”的原則,為大量第三方的庫提供自動配置的功能。由Spring專家Josh Long主導(dǎo)開發(fā)的spring-boot-starters為我們在Spring Boot下使用Activiti做了自動配置。 其中主要自動配置包括: * 自動創(chuàng)建Activiti ProcessEngine的Bean; * 所有的Activiti Service都自動注冊為Spring的Bean; * 創(chuàng)建一個Spring Job Executor; * 自動掃描并部署位于src/main/resources/processes目錄下的流程處理文件。
二、開始
1.maven添加activiti的依賴
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.21.0</version>
</dependency>
2.創(chuàng)建流程圖文件
? ? 2.1在resources下建立processes目錄
? ? 2.2在processes目錄下將bpmn、bpmn.xml或bpmn20.xml等后綴的文件(activiti-explorer流程圖導(dǎo)出文件)放入其中? ?
? ?
? ? ? ?三、編寫測試代碼
package com.real2tech.cloudreform.service;import org.activiti.engine.RuntimeService;import org.activiti.engine.TaskService;import org.activiti.engine.impl.persistence.entity.ExecutionEntity;import org.activiti.engine.task.Task;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.Map;@Servicepublic class ActivityService { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; public boolean startActivity() { logger.info("工作流啟動...."); Mapmap = new HashMap();
? ? ? ? map.put("apply", "zhangsan");
? ? ? ? map.put("approve", "lisi");
? ? ? ? // 流程啟動
? ? ? ? ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave2", map);
? ? ? ? String processId = pi1.getId();
? ? ? ? Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
? ? ? ? logger.info("task 第一步:{}", task);
? ? ? ? taskService.complete(task.getId(), map);// 完成第一步申請
? ? ? ? task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
? ? ? ? logger.info("task 第二步:{}", task);
? ? ? ? String taskId2 = task.getId();
? ? ? ? map.put("pass", false);
? ? ? ? taskService.complete(taskId2, map);// 駁回申請
? ? ? ? task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
? ? ? ? logger.info("task 第三步:{}", task);
? ? ? ? logger.info("工作流結(jié)束....");
? ? ? ? return false;
? ? }
}
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ?
? ? ? ? ? ?
? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?