Flowable实战(二)集成Springboot

本篇介绍Flowable集成Springboot的具体操作,包括创建Spingboot项目、加入Flowable依赖包、添加数据源、REST支持等内容。
摘要由CSDN通过智能技术生成

1、创建Springboot项目

  打开IDEA,通过File -> New -> Project… -> Spring Initializr 创建一个新的Springboot项目

在这里插入图片描述

  在下一个界面,填入项目名Name,JDK选择8

在这里插入图片描述

  接着,选择Springboot 2.6.2

在这里插入图片描述

  点击完成

在这里插入图片描述

  生成空的Springboot项目,pom.xml文件内容:

在这里插入图片描述

2、加入Flowable依赖包

  修改pom.xml文件

  "properties"属性下加入:

<flowable.version>6.7.2</flowable.version>

注意,请确保Flowable版本与Springboot版本匹配,否则会无法启动。查看Flowable不同版本对应的springboot版本,参考:https://blog.csdn.net/JinYJ2014/article/details/121530632

  "dependencies"属性下加入:

<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>${flowable.version}</version>
</dependency>

  这个依赖会自动向classpath添加正确的Flowable与Spring依赖。

注意:有时候,依赖JAR无法自动获取,可以右键点击项目,并选择 Maven ->Reload Project以强制手动刷新。

  现在可以编写Spring Boot应用了:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FlowableExampleApplication {
   

    public static void main(String[] args) {
   

        SpringApplication.run(FlowableExampleApplication.class, args);
    }

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
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,可以查看流程引擎的状态信息。 以上是集成FlowableSpringBoot的基本步骤。具体实现可能因应用场景和需求而异,需要根据实际情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值