关于springboot:Flowable实战2集成Springboot

8次阅读

共计 4515 个字符,预计需要花费 12 分钟才能阅读完成。

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/JinYJ20…

  ”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 反对等,这些将在前面的章节中介绍。

正文完
 0