springboot和activiti5编写一个请假工作流
时间: 2024-01-27 12:05:20 浏览: 131
一款基于Activiti工作引擎的springboot框架开发的服务于学院的请假系统+源代码+文档说明
好的,下面是基于Spring Boot和Activiti 5的请假工作流示例:
1. 首先,创建一个Spring Boot项目,并添加以下依赖:
```xml
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
</dependency>
```
2. 在项目中创建一个Activiti配置类:
```java
@Configuration
public class ActivitiConfig {
@Bean
public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource,
PlatformTransactionManager transactionManager) {
SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
processEngineConfiguration.setDataSource(dataSource);
processEngineConfiguration.setTransactionManager(transactionManager);
processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
return processEngineConfiguration;
}
@Bean
public ProcessEngineFactoryBean processEngineFactoryBean(ProcessEngineConfiguration processEngineConfiguration) {
ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
processEngineFactoryBean.setProcessEngineConfiguration(processEngineConfiguration);
return processEngineFactoryBean;
}
@Bean
public RepositoryService repositoryService(ProcessEngine processEngine) {
return processEngine.getRepositoryService();
}
@Bean
public RuntimeService runtimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
@Bean
public TaskService taskService(ProcessEngine processEngine) {
return processEngine.getTaskService();
}
@Bean
public HistoryService historyService(ProcessEngine processEngine) {
return processEngine.getHistoryService();
}
@Bean
public ManagementService managementService(ProcessEngine processEngine) {
return processEngine.getManagementService();
}
}
```
3. 创建请假流程的BPMN文件:
请假流程BPMN文件示例:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="http://www.activiti.org/test">
<process id="leaveProcess" name="请假流程" isExecutable="true">
<startEvent id="startEvent" name="开始"/>
<userTask id="applyTask" name="请假申请" activiti:assignee="${applyUser}">
<extensionElements>
<activiti:taskListener event="create" class="com.example.activiti.listener.ApplyTaskListener"/>
</extensionElements>
</userTask>
<userTask id="auditTask" name="审批" activiti:assignee="${auditUser}">
<extensionElements>
<activiti:taskListener event="create" class="com.example.activiti.listener.AuditTaskListener"/>
</extensionElements>
</userTask>
<endEvent id="endEvent" name="结束"/>
<sequenceFlow id="startToApply" sourceRef="startEvent" targetRef="applyTask"/>
<sequenceFlow id="applyToAudit" sourceRef="applyTask" targetRef="auditTask"/>
<sequenceFlow id="auditToEnd" sourceRef="auditTask" targetRef="endEvent"/>
</process>
</definitions>
```
4. 创建请假申请和审批任务的TaskListener:
```java
public class ApplyTaskListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
// 设置任务的候选人
delegateTask.addCandidateUser("user1");
delegateTask.addCandidateUser("user2");
}
}
public class AuditTaskListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
// 获取请假申请的信息
String leaveInfo = delegateTask.getVariable("leaveInfo").toString();
// TODO: 根据请假申请的信息进行审批
}
}
```
5. 创建一个请假申请的Controller:
```java
@RestController
@RequestMapping("/leave")
public class LeaveController {
@Autowired
private RuntimeService runtimeService;
@PostMapping("/apply")
public String apply(@RequestBody LeaveRequest leaveRequest) {
// 启动请假流程
Map<String, Object> variables = new HashMap<>();
variables.put("applyUser", "user");
variables.put("leaveInfo", leaveRequest.getInfo());
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess", variables);
return processInstance.getId();
}
}
```
6. 创建一个测试用例,模拟审批流程:
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class LeaveProcessTest {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Test
public void testLeaveProcess() {
// 启动请假流程
Map<String, Object> variables = new HashMap<>();
variables.put("applyUser", "user");
variables.put("auditUser", "admin");
variables.put("leaveInfo", "请假三天");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess", variables);
// 审批任务
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
taskService.claim(task.getId(), "admin");
taskService.complete(task.getId());
}
}
```
以上就是基于Spring Boot和Activiti 5的请假工作流示例,希望能够帮助到你。
阅读全文