文章目录
1. 创建Maven项目:expense。
2. 修改pom.xml,添加依赖。
pom.xml内容如下。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>expense</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<camunda.spring-boot.version>7.16.0</camunda.spring-boot.version>
<spring-boot.version>2.5.4</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>${camunda.spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. 创建启动类ExpenseApplication。
java目录下创建启动类com.example.camunda.ExpenseApplication,内容如下。
package com.example.camunda;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExpenseApplication {
public static void main(String[] args) {
SpringApplication.run(ExpenseApplication.class,args);
}
}
4. 启动应用,访问localhost:8080。
运行启动类后,访问localhost:8080,页面要求我们注册账号,不妨在application.yml中配置默认账号。
5. 在resources下创建application.yml,并添加配置。
application.yml配置的内容如下。
camunda.bpm:
admin-user:
id: demo
password: demo
firstName: Demo
filter:
create: All tasks
配置账号,账号和密码分别为demo/demo。
为任务列表配置名为"All tasks"的过滤器。
6. 重启应用。
访问localhost:8080,使用账号demo/demo可以登录。
7. 创建业务流程:报销申请->报销审批->报销已处理。
1)Start Event
Id:apply
Name:报销申请
Type:Embedded or External Task Forms
Form Key:embedded:app:forms/apply.html
2)User Task
Id:check
Name:报销审批
Assignee:demo
Type:Embedded or External Task Forms
Form Key:embedded:app:forms/check.html
3)End Event
Id:processed
Name:处理完成
4)expense_approval.bpmn
点击任意空白处,设置流程图的Id、Name,勾选Executable。
Id:expenseApproval
Name:报销审批业务流
最后,将文件另存为expense_approval.bpmn,并将该文件保存在maven项目的resources目录下。
expense_approval.bpmn的内容如下,
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions
xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
xmlns:modeler="http://camunda.org/schema/modeler/1.0"
id="Definitions_0k7pupt"
targetNamespace="http://bpmn.io/schema/bpmn"
exporter="Camunda Modeler"
exporterVersion="4.11.1"
modeler:executionPlatform="Camunda Platform"
modeler:executionPlatformVersion="7.15.0">
<bpmn:process id="expenseApproval" name="报销审批业务流" isExecutable="true">
<bpmn:startEvent id="apply" name="报销申请" camunda:formKey="embedded:app:forms/apply.html">
<bpmn:outgoing>Flow_1uukb4n</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1uukb4n" sourceRef="apply" targetRef="check" />
<bpmn:userTask id="check" name="报销审批" camunda:formKey="embedded:app:forms/check.html" camunda:assignee="demo">
<bpmn:incoming>Flow_1uukb4n</bpmn:incoming>
<bpmn:outgoing>Flow_05sxvu2</bpmn:outgoing>
</bpmn:userTask>
<bpmn:endEvent id="processed" name="处理完成">
<bpmn:incoming>Flow_05sxvu2</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_05sxvu2" sourceRef="check" targetRef="processed" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="expenseApproval">
<bpmndi:BPMNEdge id="Flow_1uukb4n_di" bpmnElement="Flow_1uukb4n">
<di:waypoint x="198" y="117" />
<di:waypoint x="260" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_05sxvu2_di" bpmnElement="Flow_05sxvu2">
<di:waypoint x="360" y="117" />
<di:waypoint x="422" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="apply">
<dc:Bounds x="162" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="159" y="142" width="45" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0w28gdb_di" bpmnElement="check">
<dc:Bounds x="260" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1dnxcab_di" bpmnElement="processed">
<dc:Bounds x="422" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="418" y="142" width="45" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
8. 创建申请表单html和审批表单html
在resources下新建目录static,在static下新建forms,在forms下新建apply.html和check.html。
<!-- apply.html -->
<form>
<div class="form-group">
<label>申请人</label>
<input type="text"
cam-variable-name="applicant"
cam-variable-type="String"
class="form-control"/>
</div>
<div class="form-group">
<label>金额</label>
<input type="text"
cam-variable-name="amount"
cam-variable-type="Double"
class="form-control"/>
</div>
</form>
<!-- check.html -->
<form>
<div class="form-group">
<label>审批意见</label>
<select cam-variable-name="approved"
cam-variable-type="Boolean"
class="form-control">
<option value="true">同意</option>
<option value="false">不同意</option>
</select>
</div>
</form>
这里的apply.html,与Form Key:embedded:app:forms/apply.html相对应,即Start Event(报销申请)时使用的apply.html。
这里的check.html,与Form Key:embedded:app:forms/check.html相对应,即User Task(报销审批)时使用的check.html。
其中,这里使用到了嵌入式表单常用控件:Text Inputs 和 Boolean Inputs。
9. 配置业务流程的自动部署
To declare the process application, just add the @EnableProcessApplication annotation on your WebappExampleProcessApplication class and put the empty processes.xml file in the src/main/resources/META-INF folder. The file is required by the Camunda Engine for every process application, but in our case it will stay empty.
要实现业务流程的自动部署,需要做以下两点。
- 在resources下新建目录META-INF,并在该目录下新建process.xml,xml文件内容可为空。
- 在启动类中添加注解@EnableProcessApplication,如下
package com.example.camunda;
import org.camunda.bpm.spring.boot.starter.annotation.EnableProcessApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableProcessApplication
public class ExpenseApplication {
public static void main(String[] args) {
SpringApplication.run(ExpenseApplication.class,args);
}
}