08-Flowable SpringBoot2.X集成

本文详细介绍如何在Spring Boot 2.X中集成Flowable工作流引擎,包括添加Maven依赖、配置application.yml文件及启动流程,解决集成过程中遇到的mysql驱动导致的表创建失败问题。

1. 添加pom依赖

主要依赖如下:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.1.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<java.version>1.8</java.version>
	<flowable.version>6.4.0</flowable.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.28</version>
	</dependency>

	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.0</version>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>


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

	<!-- 热部署 optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
		<scope>true</scope>
	</dependency>

</dependencies>

注意事项
在flowable与springboot2.x集成过程中,flowable相关表自动创建不能成功,发现是mysql驱动引起的。springboot2.x的mysql驱动是 mysql-connector-java 6(com.mysql.cj.jdbc.Driver),jar包依赖为6以上版本。将mysql驱动改为mysql-connector-java 5(com.mysql.jdbc.Driver),jar包依赖改为6以下版本,flowable相关表可以自动创建成功

2. application.yml文件配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/activiti_springboot?characterEncoding=UTF-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
#flowable:
#  database-schema-update: create-drop
 # check-process-definitions: false

3. 启动

启动引导类,启动成功后,观察mysql数据库,可以查看到自动生成的flowable相关数据表。
在这里插入图片描述

<think>首先,用户的问题是:“我想查找Spring Boot 2.x集成flowable的方法 请问Spring Boot 2.x集成flowable的具体步骤和方法”。这是一个技术问题,我需要提供Spring Boot 2.x集成Flowable工作流引擎的具体步骤。 从系统级指令中,我需要注意: - 使用$...$格式行内数学表达式,但这里可能不涉及数学相关内容。 - 独立公式用$$...$$,但同样,这个问题是技术集成,可能不需要数学公式。 - LaTeX语法正确:在代码块中确保正确。 - 使用中文回答:是的,所有回答用中文。 - 生成相关问题:在回答结束后,生成相关问题。 - 引用标识:如果引用内容,末尾添加引用标识。用户提供了引用:[^1]和[^2],但用户说“上述引用仅供参考,并不是用户真正的需求”,所以我不能直接依赖这些引用。我应该基于知识提供步骤。 用户提供的引用: - 引用[1]: Spring Boot集成Flowable实现工作流。介绍了配置引擎、编写定义、创建服务和控制器。 - 引用[2]: Spring Boot + Flowable教程,包含BPMN和CMMN整合、环境配置、流程设计等。 用户强调这些引用仅供参考,我需要参考用户的问题和我的回答。用户的问题是直接关于Spring Boot 2.x集成Flowable的方法。 我的回答结构应该清晰,逐步解决问题: 1. **介绍**:简要说明Spring Boot集成Flowable的目的。 2. **具体步骤**:详细列出集成步骤。 3. **代码示例**:提供代码片段。 4. **总结**:简短总结。 5. **相关问题**:生成相关问题。 基于我的知识: - Spring Boot集成Flowable可以通过添加依赖、配置属性、定义流程模型、实现服务和控制器。 - 步骤大致包括: - 添加Flowable依赖。 - 配置数据库(Flowable使用数据库存储流程)。 - 定义BPMN流程。 - 创建服务和控制器处理工作流任务。 - 集成安全或自定义配置。 确保使用中文。 **步骤大纲:** 1. **添加依赖**:在pom.xml中添加Flowable Spring Boot Starter。 2. **配置应用属性**:设置数据库连接等。 3. **定义流程模型**:使用BPMN文件。 4. **实现服务层**:处理流程引擎操作。 5. **创建控制器**:暴露REST API。 6. **测试运行**:启动应用并测试。 提供代码示例,使用代码块。 最后,生成相关问题。 **相关问题生成:** - 基于主题:Flowable集成、Spring Boot 2.x、优化等。 - 例如: 1. 如何自定义Flowable引擎配置? 2. Flowable中处理异常的方法? 3. 如何集成前端UI工具? 现在,构建回答。 </think>### Spring Boot 2.x 集成 Flowable 的方法 Spring Boot 2.x 集成 Flowable 工作流引擎是一个常见需求,它可以帮助您快速实现业务流程自动化。Flowable 是一个轻量级、开源的工作流引擎,支持 BPMN 2.0 标准。下面我将详细介绍在 Spring Boot 2.x 项目中集成 Flowable 的具体步骤和方法,确保步骤清晰且可操作。整个过程包括依赖添加、配置设置、流程定义和服务实现。参考您的需求,我将优先基于 Spring Boot 2.x 的兼容性进行说明,并结合常见最佳实践[^1]。 #### 集成步骤和方法 以下是 Spring Boot 2.x 集成 Flowable 的核心步骤,基于 Maven 项目管理工具。假设您已有一个 Spring Boot 2.x 项目(版本如 2.7.x 或 2.6.x)。 1. **添加 Flowable 依赖** 在项目的 `pom.xml` 文件中,添加 Flowable Spring Boot Starter 依赖。确保选择与 Spring Boot 2.x 兼容的 Flowable 版本(例如 Flowable 6.7.x)。 ```xml <dependencies> <!-- Spring Boot Starter Web (用于 REST 控制器) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Flowable Spring Boot Starter --> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-spring-boot-starter</artifactId> <version>6.7.0</version> <!-- 兼容 Spring Boot 2.x --> </dependency> <!-- 数据库依赖(如 H2 用于测试或 MySQL 用于生产) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 注意:Flowable 需要数据库存储流程数据,H2 适用于开发和测试,生产环境建议使用 MySQL 或 PostgreSQL。添加数据库驱动后,Flowable 会自动初始化数据库表结构。 2. **配置应用属性** 在 `application.properties` 或 `application.yml` 文件中,设置数据库连接和 Flowable 引擎参数。Spring Boot 会自动配置 Flowable 引擎。 ```properties # 数据库配置(以 H2 为例) spring.datasource.url=jdbc:h2:mem:flowable-db;DB_CLOSE_DELAY=-1 spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= # Flowable 引擎配置 flowable.async-executor-activate=false # 禁用异步执行器(简化测试) flowable.database-schema-update=true # 自动更新数据库表结构 ``` 如果使用 MySQL,替换数据库 URL 为 `jdbc:mysql://localhost:3306/flowable_db`,并添加 MySQL 驱动依赖[^2]。 3. **定义工作流模型(BPMN)** 创建 BPMN 2.0 流程定义文件,描述业务流程(如审批流程)。在 `src/main/resources` 下新建 `processes` 文件夹,并添加一个 BPMN 文件(如 `leave-request.bpmn20.xml`)。 ```xml <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" targetNamespace="http://flowable.org/bpmn"> <process id="leaveProcess" name="Leave Request Process"> <startEvent id="startEvent" /> <userTask id="managerApproval" name="Manager Approval" /> <sequenceFlow sourceRef="startEvent" targetRef="managerApproval" /> <endEvent id="endEvent" /> <sequenceFlow sourceRef="managerApproval" targetRef="endEvent" /> </process> </definitions> ``` Flowable 在启动时会自动扫描 `resources/processes` 目录并部署流程。 4. **实现服务层** 创建服务类来处理流程引擎操作,如启动流程、查询任务。使用 Spring 的 `@Service` 注解。 ```java import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class WorkflowService { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; // 启动一个业务流程实例 public void startProcess(String processDefinitionKey) { runtimeService.startProcessInstanceByKey(processDefinitionKey); } // 查询用户任务 public List<Task> getUserTasks(String assignee) { return taskService.createTaskQuery().taskAssignee(assignee).list(); } } ``` 5. **创建控制器层** 开发 REST 控制器提供 API 接口,用于前端交互或外部调用。 ```java import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; @RestController @RequestMapping("/api/workflow") public class WorkflowController { @Autowired private WorkflowService workflowService; @PostMapping("/start") public String startProcess(@RequestParam String processKey) { workflowService.startProcess(processKey); return "Process started successfully!"; } @GetMapping("/tasks") public List<Task> getUserTasks(@RequestParam String assignee) { return workflowService.getUserTasks(assignee); } } ``` 6. **测试运行** - 启动 Spring Boot 应用:运行 `main` 类(如 `@SpringBootApplication` 注解的类)。 - 访问 API:使用工具(如 Postman)发送请求,例如 `POST http://localhost:8080/api/workflow/start?processKey=leaveProcess`。 - 检查数据库:Flowable 会创建 ACT_* 表存储流程数据,确保配置正确。 #### 注意事项 - **兼容性**:Spring Boot 2.x 与 Flowable 6.7.x 兼容良好。避免使用 Flowable 7.x(主要针对 Spring Boot 3.x)。 - **安全集成**:如果需要,添加 Spring Security 依赖来保护工作流 API。 - **监控和优化**:生产环境中,启用 Flowable 的管理控制台或集成监控工具。 - 以上步骤基于简化示例,实际项目可根据需求扩展 BPMN 模型或添加 CMMN 支持[^2]。 集成完成后,您就能通过 Spring Boot 高效管理业务流程,如请假审批或订单处理。整个过程注重自动化和可扩展性,确保开发效率[^1]。 --- ### 相关问题 如果您需要进一步探索,以下是基于本主题的相关问题: 1. 如何在 Spring Boot 2.x 中优化 Flowable 的性能? 2. Flowable 工作流如何处理异常和回滚事务? 3. 如何将 Flowable 与 Spring Security 集成以实现任务权限控制? 4. Flowable 支持哪些高级功能如历史数据查询和流程监控? 5. 在 Spring Boot 项目中,如何测试 Flowable 工作流的完整生命周期?
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥俊子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值