Flowable框架的入门与使用【二】

511 阅读4分钟

通过上一篇文章,我们对flowable框架已经有了大致的了解,本篇将介绍如果在springboot中集成及应用。

在集成flowable设计器的时候,可谓是全是坑,走一步一个坑,难死了,这里问题不一一表述,详情看参考资料中的B站大佬视频

本文项目代码已开源,地址:gitee.com/szwei/sprin…

image.png

一、新建springboot项目,引入依赖

  1. 首先新建一个springboot项目,版本选择2.X,因为3.X的java最低版本要求都17了。引入flowable的版本为6.5.0

  2. 引入依赖

     <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    ​
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <scope>runtime</scope>
            </dependency>
    ​
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatisplus.version}</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</artifactId>
                <version>${flowable.version}</version>
            </dependency>
    ​
            <dependency>
                <groupId>org.flowable</groupId>
                <artifactId>flowable-ui-modeler-rest</artifactId>
                <version>${flowable.version}</version>
            </dependency>
        </dependencies>
    

    如果对 mysql-connector-j 不太理解的话,在Spring Boot 2.7.8之后,更改 mysql:mysql-connector-java -> com.mysql:mysql-connector-j 并删除 mysql-connector-java 依赖管理,官方删除了原来的

  3. 配置文件

    server:
      port: 8080
    
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/flowable
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    flowable:
      common:
        app:
          idm-url: http://127.0.0.1:8080/flowable-idm
    
    
  4. 启动类配置扫描

    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
    @ComponentScan(basePackages = {"org.flowable.ui.modeler","org.flowable.ui.common","com.example.springboot_flowable.flowable"},
            excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {org.flowable.ui.common.rest.idm.remote.RemoteAccountResource.class})})
    public class SpringbootFlowableApplication {
    ​
        public static void main(String[] args) {
            SpringApplication.run(SpringbootFlowableApplication.class, args);
        }
    ​
    }
    

    说明:这里配置了不扫描 SpringSecurity的自动配置类,因为我们的系统没有使用 SpringSecurity,从而用不到。扫描ui.modeler包,扫描自己的包,排除掉 RemoteAccountResource 这个类,为什么要排除呢,因为 /app/rest/account,这个接口在这个类中,如果不排除掉的话,就会被spring扫描到,而我们没有使用SpringSecurity,默认的不能用,所以不扫描。我们后面会单独写一个接口

二、引入UI文件,集成设计器

  1. 从maven中央仓库下载了一个6.5.0版本的,将里面的static目录直接复制到了项目的static目录下,地址为:repo1.maven.org/maven2/org/…

  2. 启动项目之后,直接访问:http://localhost:8080/index.html,会显示空白页,并且会有两个请求错误,显示 404 ,请求如下

    http://localhost:8080/index.html/app/rest/account

    http://localhost:8080/index.html/views/processes.html

    原因是引入的ui静态文件中,app-cfg.js 文件中,contextRoot 和 webContextRoot 配置的为 pathname,他是一个表达式,我们将contextRoot 和 webContextRoot的值都改为'',再次访问即可

  1. 引入 flowable-ui-modeler-rest 依赖,该依赖是新建流程请求的接口

  2. 重写/app/rest/account 接口,并扫描

        @GetMapping(value = {"/rest/account"},produces = {"application/json"})
        public UserRepresentation getAccount() {
            User user = new UserEntityImpl();
            user.setId("123456");
            SecurityUtils.assumeUser(user);
    ​
            UserRepresentation userRepresentation = new UserRepresentation();
            userRepresentation.setId("123456");
            userRepresentation.setFirstName("zhangsan");
            List<String> privileges = new ArrayList<>();
            privileges.add("flowable-idm");
            privileges.add("flowable-modeler");
            privileges.add("flowable-task");
            userRepresentation.setPrivileges(privileges);
            return userRepresentation;
        }
    

    主要是返回一个 UserRepresentation,设置一下ID、name、权限就可以,然后打开http://localhost:8080/index.html之后,就可以看到右上角显示的用户名为zhangsan

  3. 访问流程设计器,可以创建流程,保存流程

  4. 选择用户时,调用的接口是:app/rest/editor-groups,该接口在 EditorGroupsResource 类中,同样会查询不到用户,一般我们是需要我们自己的用户体系的,所以也需要重写这个接口,这样在流程设计器上分配用户的时候可以选择我们自己的用户体系

  5. 这样flowable流程设计器就可以打开了

三、我对flowable的理解使用

  1. 可以设置任务监听器

    可以为单个任务设置监听器,也可以设置执行监听器,当任务执行或者结束的时候统一执行某个动作

  2. 流程设计器一般和动态表单结合使用,流程不关心表单的内容,表单是可以随意拖拽的,存储时可以是个json

  3. 流程可以设置成多实例,比如某个节点有多个人审批,竞争审批或者都审批通过时,进入下一个节点。具体多实例网上有大把的案例,他就像一个for循环,设置一个List集合后,遍历。当多实例时,流程进入到该节点时,会在ACT_RU_TASK表生成多个任务,每个用户一个。

  4. 流程条件通常是通过流程变量来控制的,通过变量的值,来控制分支

  5. flowable是一个流程引擎,它功能非常强大,可以通过各种api来操作引擎。但具体使用过程中,不能一直调用API,官方API也可能不太满足具体的需求,所以需要结合flowable引擎来设计自己的表,将流程引擎关联到自身的表,这样既能满足需求也不用频繁调用api,表结构需要好好设计一下。

四、参考资料

  1. www.bilibili.com/video/BV1rJ…