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);
}
}
Flowable需要數據庫來存儲數據。運行上面的代碼會得到異常提示,指出需要在classpath中添加數據庫驅動依賴。
3、添加數據源
現在添加MySQL數據庫依賴:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
注意:MySQL依賴包版本根據自己所連接的數據庫版本修改,否則可能會連接失敗
application.properties文件中添加數據源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flowable?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=jinyangjie
spring.datasource.password=jinyangjie
應用成功啟動后,查看數據庫,可以看到,已經自動創建了Flowable表:
注意:如果出現“Caused by: java.lang.RuntimeException: Exception while initializing Database connection”的錯誤,請確保數據源的配置項正確,並檢查MySQL依賴包版本是否匹配
4、REST支持
4.1 添加REST依賴
通常我們的應用會使用REST API。添加下列依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<spring.boot.version>2.6.2</spring.boot.version>
下面做個Controller和Service層的簡單使用示例,例子來源於Flowable官方文檔。
4.2 添加流程文件
resources/processes目錄下的任何BPMN 2.0流程定義都會被自動部署。創建processes目錄,並在其中創建示例流程定義(命名為one-task-process.bpmn20.xml)。
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">
<process id="oneTaskProcess" name="The One Task Process">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task" flowable:assignee="jinyangjie" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
4.3 serivice層代碼示例
創建一個新的Spring服務類,並創建兩個方法:一個用於啟動流程,另一個用於獲得給定任務辦理人的任務列表。在這里只是簡單地包裝了Flowable調用,在實際使用場景中會比這復雜得多。
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class MyService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Transactional
public void startProcess() {
runtimeService.startProcessInstanceByKey("oneTaskProcess");
}
@Transactional
public List<Task> getTasks(String assignee) {
return taskService.createTaskQuery().taskAssignee(assignee).list();
}
}
4.4 controller層代碼示例
@RestController
public class MyRestController {
@Autowired
private MyService myService;
@RequestMapping(value="/process", method= RequestMethod.POST)
public void startProcessInstance() {
myService.startProcess();
}
@RequestMapping(value="/tasks", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public List<TaskRepresentation> getTasks(@RequestParam String assignee) {
List<Task> tasks = myService.getTasks(assignee);
List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
static class TaskRepresentation {
private String id;
private String name;
public TaskRepresentation(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Spring Boot會自動掃描組件,並找到我們添加在應用類上的@Service與@RestController。再次運行應用,現在可以與REST API交互了。例如使用cURL:
curl http://localhost:8080/tasks?assignee=jinyangjie
[]
curl -X POST http://localhost:8080/process
curl http://localhost:8080/tasks?assignee=jinyangjie
[{"id":"b6350a6d-7070-11ec-bd1b-0a0027000006","name":"my task"}]
5、小結
本篇介紹了Springboot的初步集成,很明顯還有很多Spring Boot相關的內容還沒有提及,比如打包WAR文件、Spring Security支持等,這些將在后面的章節中介紹。