1.导入依赖
<!-- activiti工作流 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.14</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.14</version>
</dependency>
2.配置xml
a.新建spring-activiti.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="activityFontName" value="微软雅黑"></property> <!-- 引入druid数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入spring事务管理 -->
<property name="transactionManager" ref="txManager"></property>
<!-- 建表策略 -->
<property name="databaseSchemaUpdate" value="true"></property>
<!-- 历史控制级别 -->
<property name="history" value="full"></property> <!-- 自动部署 -->
<property name="deploymentResources">
<list>
<value>classpath*:MyProcess.bpmn</value>
</list>
</property>
</bean> <bean id="processEngineFactoryBean" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
</bean> <bean id="repositoryService" factory-bean="processEngineFactoryBean" factory-method="getRepositoryService"></bean>
<bean id="runtimeService" factory-bean="processEngineFactoryBean" factory-method="getRuntimeService"></bean>
<bean id="taskService" factory-bean="processEngineFactoryBean" factory-method="getTaskService"></bean>
<bean id="historyService" factory-bean="processEngineFactoryBean" factory-method="getHistoryService"></bean>
<bean id="formService" factory-bean="processEngineFactoryBean" factory-method="getFormService"></bean>
<bean id="identityService" factory-bean="processEngineFactoryBean" factory-method="getIdentityService"></bean>
<bean id="managementService" factory-bean="processEngineFactoryBean" factory-method="getManagementService"></bean> </beans>
注意:其中 dataSource 和 txManager 为 spring-mybatis.xml 中配置的 “数据源(数据库连接池)” 和 “事务管理”,所以不能在web.xml中引入,而应在spring-mybatis.xml中引入
b.在 spring-mybatis.xml 引入 spring-activiti.xml 配置
<!-- 导入activiti工作流配置 -->
<import resource="spring-activiti.xml"/>
3.绘制流程图
a.eclipse 中安装插件: help —> Install New Software —> Add
Name:activiti
Location:http://activiti.org/designer/update/
b.新建流程图:新建 MyProcess.bpmn
4.使用
a.在Service中使用
@Service
public class ProcessService { public final static String PROCESS_ID = "myProcess"; //流程储存服务组件
@Resource
RepositoryService repositoryService; //运行时服务组件
@Resource
RuntimeService runtimeService; //流程中的任务TASK组件
@Resource
TaskService taskService; //部署流程(若xml已配置自动部署,则不需要此方法)
public void deployProcess(){
// 部署流程,只要是符合BPMN2规范的XML文件,理论上都可以被ACTIVITI部署
repositoryService.createDeployment().addClasspathResource("MyProcess.bpmn").deploy();
} //启动流程
public void startProcess(){
// 开启流程,参数是流程的ID
runtimeService.startProcessInstanceByKey(PROCESS_ID);
} //根据Assignee查询task
public List<Task> findTaskByAssignee(String assignee){
List<Task> taskList = taskService.createTaskQuery().
taskAssignee(assignee). //根据办理人查询
orderByTaskCreateTime().asc(). //根据创建时间升序排列
list(); //返回列表 for(Task task : taskList){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("=========================================");
} return taskList;
} //完成任务
//variables 为流程变量,对应bpmn 中 Condition 设置的 ${message=='不重要'}
public void completeProcess(String taskId, Map<String, Object> variables){
taskService.complete(taskId, variables);
System.out.println("完成任务ID:"+taskId);
}
}
b.在Controller中测试
//提交流程
@RequestMapping("submitProcess")
public @ResponseBody String submitProcess(HttpServletRequest request, HttpServletResponse response){
processService.startProcess();
return "提交流程成功";
} //部长审批
@RequestMapping("departmentProcess")
public @ResponseBody String departmentProcess(HttpServletRequest request, HttpServletResponse response){
List<Task> taskIdList = processService.findTaskByAssignee("department");
for(Task task : taskIdList){
String taskId = task.getId();
processService.completeProcess(taskId, null);
}
return "部长审批成功";
} //副总审批
@RequestMapping("managerProcess")
public @ResponseBody String managerProcess(HttpServletRequest request, HttpServletResponse response){
List<Task> taskIdList = processService.findTaskByAssignee("manager");
for(Task task : taskIdList){
String taskId = task.getId();
processService.completeProcess(taskId, null);
}
return "副总审批成功";
}