您好,登錄后才能下訂單哦!
這篇文章主要講解了Springboot集成activity的用法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
1.第一步添加bpmn文件得插件,不然沒法查看和編輯bpmn文件,添加插件的方法各自百度即可,很簡單
2.安裝好bpmn插件后開始新建bpmn文件,也就是畫流程圖
一般是在代碼中進行指定流程審批人的,我這里只做入門案例
3.添加pom依賴
這里包含了在線設計流程圖的相關依賴,在下一篇會寫出來,項目中一般都是在前端在線設計;所以直接放上去也沒事
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com</groupId> <artifactId>springboot_activiti</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_activiti</name> <description>Demo project for Spring Boot</description> <properties> <springboot.version>2.2.0.RELEASE</springboot.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <activiti.version>6.0.0</activiti.version> </properties> <dependencies> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti.version}</version> <exclusions> <exclusion> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-jdbc</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-tx</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-orm</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>mybatis</artifactId> <groupId>org.mybatis</groupId> </exclusion> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>${activiti.version}</version> </dependency> <!-- Activiti 流程圖 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-common-rest</artifactId> <version>${activiti.version}</version> </dependency> <!-- Activiti 在線設計 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-modeler</artifactId> <version>5.22.0</version> <exclusions> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-tx</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-web</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-security-config</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-core</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-crypto</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-web</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-webmvc</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> <exclusion> <artifactId>commons-io</artifactId> <groupId>commons-io</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>${springboot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8081</port> <path>/activity</path> <systemProperties> <com.sun.management.jmxremote.port>4000</com.sun.management.jmxremote.port> </systemProperties> </configuration> </plugin> </plugins> </build> </project>
4.yml文件
這里采用的數(shù)據(jù)庫還是mysql
這里有個需要注意的地方,springboot2.2中的driver-class-name值變了,不是以前的那個了,不然會報錯
spring:
datasource:
url: jdbc:mysql://localhost:3307/t2?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
activiti:
# 自動部署驗證設置:true-開啟(默認)、false-關閉
check-process-definitions: false
#自動部署文件路徑后綴
# process-definition-location-prefix: classpath:/processes/
# process-definition-location-suffixes:
# - **.bpmn
# - **.bpmn20.xml
server:
port: 8082
5.配置已經好了,下面開始寫代碼了,只是初步寫了一些新建流程,正常提交流程,直接結束流程,以及查詢待辦的案例,細節(jié)還需要各自自己去調試學習
controller
package com.springboot_activiti.controller; import com.springboot_activiti.service.ActivityConsumerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/activiti") public class ActivitiController { @Autowired private ActivityConsumerService activityConsumerService; /** * 注冊流程 * @return */ @RequestMapping("/createDeployment") public Boolean createDeployment(){ return activityConsumerService.createDeployment(); } /** * 啟動流程 * @return */ @RequestMapping("/startActivityDemo") public Boolean startActivityDemo(){ return activityConsumerService.startActivityDemo("test01"); } /** * 獲取待辦 * @return */ @RequestMapping("/getTaskList") public boolean getTaskList(){ return activityConsumerService.getTaskList(); } /** * 提交 * @param taskId * @return */ @RequestMapping("/complete") public boolean complete(String taskId){ return activityConsumerService.complete(taskId); } /** * 根據(jù)流程id直接結束流程 * @paramru * @return */ @RequestMapping("/deleteProcessInstance") public boolean deleteProcessInstance(String runId){ return activityConsumerService.deleteProcessInstance(runId); } }
service
package com.springboot_activiti.service; public interface ActivityConsumerService { boolean createDeployment(); boolean startActivityDemo(String key); boolean getTaskList(); boolean complete(String taskId); boolean deleteProcessInstance(String runId); }
實現(xiàn)類
package com.springboot_activiti.service; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("activityService") public class ActivityConsumerServiceImpl implements ActivityConsumerService { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private RepositoryService repositoryService; /** * 注冊一個流程 * @return */ @Override public boolean createDeployment() { DeploymentBuilder builder=repositoryService.createDeployment(); builder.addClasspathResource("test01.bpmn"); builder.deploy(); return true; } /** * 查詢待辦 * @return */ public boolean getTaskList(){ //獲取待辦的一些信息,這里可以傳入需要查詢的用戶, //我這里查詢的所有待辦 List<Task> tasks = taskService.createTaskQuery().list(); for (Task t:tasks) { System.out.println(t.getCreateTime()); System.out.println(t.getId()); System.out.println(t.getName()); System.out.println(t.getProcessInstanceId()); System.out.println(t.getTaskDefinitionKey()); System.out.println(t.getParentTaskId()); } return true; } /** * 根據(jù)流程key開啟一個流程 * @param key * @return */ @Override public boolean startActivityDemo(String key) { ProcessInstance test01 = runtimeService.startProcessInstanceByKey(key); String id = test01.getId(); System.out.println("流程id="+id); /* */ /* String assignee = "abc";*/ return true; } /** * 根據(jù)任務id提交任務 * @param taskId * @return */ @Override public boolean complete(String taskId){ taskService.complete(taskId); return true; } /** * 根據(jù)流程id直接結束流程 * @param runId * @return */ @Override public boolean deleteProcessInstance(String runId){ runtimeService.deleteProcessInstance(runId,"結束"); return true; } }
啟動類,這里存在一個在線視圖設計的驗證處理,
package com.springboot_activiti; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com") @SpringBootApplication @EnableAutoConfiguration(exclude = { org.activiti.spring.boot.SecurityAutoConfiguration.class }) public class SpringbootActivitiApplication { public static void main(String[] args) { SpringApplication.run(SpringbootActivitiApplication.class, args); } }
所有的代碼已經寫好了
備注說明:
項目啟動會新建28張表,各自代表的含義自行百度把
因為我的數(shù)據(jù)都是直接在流程圖bpmn文件里面寫好的,實際中肯定是在代碼里面指定的
首先訪問注冊流程
啟動流程(會返回一個流程id)
查看待辦就可以獲取相關的信息了
正常提交任務,會審批到下一節(jié)點(查看待辦可以得到任務id)
直接結束流程(非正常結束,啟動流程的id傳入即可)
請求案例
查詢待辦
啟動流程
看完上述內容,是不是對Springboot集成activity的用法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。