flowable如何集成springboot
时间: 2023-11-08 09:56:12 浏览: 225
09-springbooOA办公系统+基于RuoYi开发+flowable
5星 · 资源好评率100%
1. 添加依赖
在`pom.xml`文件中添加以下依赖:
```xml
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
```
2. 配置数据库
在`application.yml`文件中添加以下配置:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
```
3. 配置流程引擎
在`application.yml`文件中添加以下配置:
```yaml
flowable:
database-schema-update: true
history-level: full
```
4. 自定义流程引擎配置
可以通过继承`ProcessEngineConfigurationConfigurer`接口来自定义流程引擎配置,例如:
```java
@Configuration
public class FlowableConfig {
@Bean
public ProcessEngineConfigurationConfigurer processEngineConfigurationConfigurer() {
return processEngineConfiguration -> {
processEngineConfiguration.setAsyncExecutorActivate(false);
processEngineConfiguration.setActivityFontName("宋体");
processEngineConfiguration.setLabelFontName("宋体");
};
}
}
```
5. 编写流程定义
在`src/main/resources/processes`目录下创建流程定义文件,例如`leave.bpmn20.xml`:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.flowable.org/processdef">
<process id="leave" name="请假流程" isExecutable="true">
<startEvent id="start" name="开始"/>
<userTask id="apply" name="申请请假">
<extensionElements>
<flowable:formProperty id="reason" name="请假原因" type="string" required="true"/>
<flowable:formProperty id="days" name="请假天数" type="long" required="true"/>
</extensionElements>
<documentation>请假申请节点,需要填写请假原因和请假天数</documentation>
<incoming>startToApply</incoming>
<outgoing>applyToApprove</outgoing>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>applyer</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<userTask id="approve" name="审批">
<extensionElements>
<flowable:formProperty id="result" name="审批结果" type="enum" required="true">
<flowable:value id="pass" name="通过"/>
<flowable:value id="reject" name="驳回"/>
</flowable:formProperty>
<flowable:formProperty id="comment" name="审批意见" type="string" required="false"/>
</extensionElements>
<documentation>审批节点,需要填写审批结果和审批意见</documentation>
<incoming>applyToApprove</incoming>
<outgoing>approveToEnd</outgoing>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>approver</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<endEvent id="end" name="结束">
<incoming>approveToEnd</incoming>
</endEvent>
<sequenceFlow id="startToApply" sourceRef="start" targetRef="apply"/>
<sequenceFlow id="applyToApprove" sourceRef="apply" targetRef="approve"/>
<sequenceFlow id="approveToEnd" sourceRef="approve" targetRef="end"/>
</process>
<bpmndi:BPMNDiagram>
<bpmndi:BPMNPlane bpmnElement="leave">
<bpmndi:BPMNShape id="start_shape" bpmnElement="start">
<omgdc:Bounds x="180" y="80" width="50" height="50"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="apply_shape" bpmnElement="apply">
<omgdc:Bounds x="250" y="60" width="100" height="80"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="approve_shape" bpmnElement="approve">
<omgdc:Bounds x="400" y="60" width="100" height="80"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="end_shape" bpmnElement="end">
<omgdc:Bounds x="550" y="80" width="50" height="50"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="startToApply_edge" bpmnElement="startToApply">
<omgdi:waypoint x="205" y="105"/>
<omgdi:waypoint x="250" y="100"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="applyToApprove_edge" bpmnElement="applyToApprove">
<omgdi:waypoint x="350" y="100"/>
<omgdi:waypoint x="400" y="100"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="approveToEnd_edge" bpmnElement="approveToEnd">
<omgdi:waypoint x="500" y="100"/>
<omgdi:waypoint x="550" y="105"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
```
6. 编写流程服务
编写流程服务,例如:
```java
@Service
public class LeaveService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
public void startProcess(String applyer, String reason, long days) {
Map<String, Object> variables = new HashMap<>();
variables.put("applyer", applyer);
variables.put("reason", reason);
variables.put("days", days);
runtimeService.startProcessInstanceByKey("leave", variables);
}
public void approveTask(String taskId, String result, String comment) {
Map<String, Object> variables = new HashMap<>();
variables.put("result", result);
variables.put("comment", comment);
taskService.complete(taskId, variables);
}
public List<Task> getTasks(String assignee) {
return taskService.createTaskQuery().taskAssignee(assignee).list();
}
}
```
7. 测试流程服务
编写测试类,例如:
```java
@SpringBootTest
@RunWith(SpringRunner.class)
public class LeaveServiceTest {
@Autowired
private LeaveService leaveService;
@Test
public void testLeaveProcess() {
String applyer = "张三";
String reason = "生病了";
long days = 3;
leaveService.startProcess(applyer, reason, days);
List<Task> tasks = leaveService.getTasks("approver");
Assert.assertNotNull(tasks);
Assert.assertEquals(1, tasks.size());
Task task = tasks.get(0);
Assert.assertEquals(reason, task.getFormProperty("reason").getValue());
Assert.assertEquals(days, task.getFormProperty("days").getValue());
leaveService.approveTask(task.getId(), "pass", "同意");
tasks = leaveService.getTasks("approver");
Assert.assertEquals(0, tasks.size());
}
}
```
8. 运行应用程序
运行应用程序,访问http://localhost:8080/actuator/flowable,可以查看流程引擎的状态信息。
以上是集成Flowable和SpringBoot的基本步骤。具体实现可能因应用场景和需求而异,需要根据实际情况进行调整和优化。
阅读全文