Activiti————框架搭建

在项目中,经常会用到流程的技术,可是自己写又非常麻烦,所以就有Activiti框架,为我们提供了一套比较完善的流程技术,在以后的项目中也会经常用到的。大家一起来学习一下吧

本项目已经全部上传github,项目地址:

https://github.com/876745833/activiti-demo/tree/master

介绍

Activiti是一个轻量级的工作流和业务流程管理(BPM)平台,面向业务人员,开发人员和系统管理员。它的核心是用于Java的超快速,坚固的BPMN 2流程引擎。它是开源的,并根据Apache许可进行分发。Activiti可以在任何Java应用程序,服务器,集群或云中运行。它与Spring完美集成,非常轻巧,基于简单的概念。官方git地址

简单的说,就是一套工作申请用到框架。比如请假申请,是要经过层层的审核过程,这个时候就可以选择使用这个框架了,他本身帮我们创建的26张数据表(新版本或许更多),提供的一些方法让我们这些程序员的开发变的更加方便、快捷。

话不多说,下边就可以正式进入框架的学习吧。

下边是搭建框架,先做好准备工作

导入jar包

在maven项目中导入,在这里我们用的是 5.x版本 的 Mysql 数据库5.x版本的Activiti

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <!--activiti-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>5.22.0</version>
        </dependency>
        <!--用到Activiti的ID策略的时候需要加-->
        <!--<dependency>-->
            <!--<groupId>com.fasterxml.uuid</groupId>-->
            <!--<artifactId>java-uuid-generator</artifactId>-->
            <!--<version>3.1.3</version>-->
        <!--</dependency>-->

添加XML配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 配置对应数据库 -->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?characterEncoding=utf8"/>
        <property name="jdbcUsername" value="root"/>
        <property name="jdbcPassword" value="root"/>
         <!--
         设置 表创建规则
         databaseSchemaUpdate: 设置流程引擎启动和关闭时如何处理数据库表。
         false(默认):检查数据库表的版本和依赖库的版本, 如果版本不匹配就抛出异常。
         true: 构建流程引擎时,执行检查,如果需要就执行更新。 如果表不存在,就创建。
         create-drop: 构建流程引擎时创建数据库表, 关闭流程引擎时删除这些表。
         -->
        <property name="databaseSchemaUpdate" value="true"/>
        <!-- 是否使用activiti自带用户:用视图替换activiti用户 -->
        <!--<property name="dbIdentityUsed" value="false"></property>-->
        <!-- 主键生成策略 UUID -->
        <!--<property name="idGenerator">-->
            <!--<bean class="org.activiti.engine.impl.persistence.StrongUuidGenerator" />-->
        <!--</property>-->
        <!-- 自定义主键生成策略 -->
        <property name="idGenerator"><bean class="TaskIdGenerator"/> </property>
    </bean>

    <!-- 使用工厂创建流程引擎对象 -->
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>


    <!-- 注入bean 方便直接调用 (可选) -->
    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
    <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
    <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
</beans>
在配置中创建流程图

如下是写的一个Demo流程图,直接在resources中创建一个leave.xml配置文件写入。

之后更改配置文件的后缀名,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" 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:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1570697036721" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="groupTaskDelegate" isClosed="false" isExecutable="true" name="groupTaskDelegateProcess" processType="None">
    <startEvent id="startevent1" name="Start"/>
    <userTask  activiti:candidateGroups="部门主管" activiti:exclusive="true" id="usertask1" name="部门主管审批"/>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"/>
    <userTask  activiti:candidateGroups="部门经理" activiti:exclusive="true" id="usertask2" name="部门经理审批"/>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"/>
    <userTask  activiti:candidateGroups="CTO" activiti:exclusive="true" id="usertask3" name="技术总监审批"/>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"/>
    <endEvent id="endevent1" name="End"/>
    <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="groupTaskDelegate">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="Shape-startevent1">
        <omgdc:Bounds height="32.0" width="32.0" x="290.0" y="100.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="Shape-usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="255.0" y="200.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="105.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="Shape-usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="255.0" y="310.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="105.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="Shape-usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="257.0" y="420.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="105.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="Shape-endevent1">
        <omgdc:Bounds height="32.0" width="32.0" x="292.0" y="510.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1" sourceElement="startevent1" targetElement="usertask1">
        <omgdi:waypoint x="306.0" y="132.0"/>
        <omgdi:waypoint x="306.0" y="200.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2" sourceElement="usertask1" targetElement="usertask2">
        <omgdi:waypoint x="307.5" y="255.0"/>
        <omgdi:waypoint x="307.5" y="310.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3" sourceElement="usertask2" targetElement="usertask3">
        <omgdi:waypoint x="308.5" y="365.0"/>
        <omgdi:waypoint x="308.5" y="420.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4" sourceElement="usertask3" targetElement="endevent1">
        <omgdi:waypoint x="308.0" y="475.0"/>
        <omgdi:waypoint x="308.0" y="510.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

安装插件(Idea版本)

1571992056043.png

安装之后,再打开bpmn文件 流程图 如下:

1571991986776.png

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐