springboot2整合activiti7具体步骤
写在前面
需要提前了解的内容有 springboot、springSecurity、activiti基本使用
关于activiti
Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构,提供技术实现。
activiti主要应用场景
需要动态地改变流程的业务流程场景。例如请假流程、项目审批流程等。
正题
第一步:
新建一个springboot项目,引入activiti-spring-boot-starter和你所使用的数据库驱动器。本文采用Mysql 8.
读者也可以直接复制下面的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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pers.lbf</groupId>
<artifactId>springboot-activiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-activiti</name>
<description>Demo project for Spring Boot</description> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency> <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency> <dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.0.0.Beta2</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
第二步,修改application.yml配置文件
spring:
datasource:
url: jdbc:mysql://127.0.0.1:/你的数据库?userSSL=false&serverTimezone=GMT%2B8
username: root
password: 你的密码
driver-class-name: com.mysql.cj.jdbc.Driver
activiti: database-schema-update: true
db-history-used: true
history-level: audit
第三步,添加springSecurity的相关配置。
因为activiti7与springboot整合之后,默认情况下集成了springSecurity框架,所以我们需要配置一下springSecurity。本文的核心不在springSecurity,所以这一步将通过添加一个工具类的方式来完成。
package pers.lbf.springbootactiviti.utils; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component; import javax.security.auth.Subject;
import javax.vecmath.Tuple2d;
import java.util.Collection; /**
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-08-18 11:27:31
* @version 1.0
*/
@Component
public class SecurityUtil { @Autowired
private UserDetailsService userDetailsService; public void logInAs(String username) { UserDetails user = userDetailsService.loadUserByUsername(username);
if (user == null) {
throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
} Authentication authentication = new Authentication() { @Override
public String getName() { return user.getUsername();
} @Override
public boolean implies(Subject subject) {
return false;
} @Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return user.getAuthorities();
} @Override
public Object getCredentials()
{
return user.getPassword();
} @Override
public Object getDetails() { return user;
} @Override
public Object getPrincipal() { return user;
} @Override
public boolean isAuthenticated() {
return true;
} @Override
public void setAuthenticated(boolean b) throws IllegalArgumentException { }
}; SecurityContextImpl securityContext = new SecurityContextImpl();
securityContext.setAuthentication(authentication);
SecurityContextHolder.setContext(securityContext);
org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
}
}
第四步,添加权限配置信息。
正常项目中,权限信息存储于数据库当中。本文为了陈述方便,直接将权限信息定义在配置类当中。
package pers.lbf.springbootactiviti.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors; /**
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/8/18 11:40
*/
@Configuration
public class ApplicationConfig { @Bean
public UserDetailsService getUserDetailsService(){ InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager(); String[][] usersGroupsAndRoles = {
{"salaboy", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"ryandawsonuk", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"erdemedeiros", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam"},
{"system", "password", "ROLE_ACTIVITI_USER"},
{"admin", "password", "ROLE_ACTIVITI_ADMIN"}
}; for (String[] user : usersGroupsAndRoles) {
List<String> authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user,2,user.length)); userDetailsManager.createUser(new User(
user[0],
passwordEncoder().encode(user[1]),
authoritiesStrings.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList())
)
); } return userDetailsManager;
} @Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
第五步,整合Junit,验证是否整合成功
首先,添加Junit依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
编写测试类
package pers.lbf.springbootactiviti.activititest; import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.model.payloads.StartProcessPayload;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.ClaimTaskPayloadBuilder;
import org.activiti.api.task.model.builders.CompleteTaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import pers.lbf.springbootactiviti.utils.SecurityUtil; import java.util.List; /**
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/8/18 12:10
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Activiti7SpringbootTest { @Autowired
private ProcessRuntime processRuntime;
@Autowired
private TaskRuntime taskRuntime; @Autowired
private SecurityUtil securityUtil; @Autowired
private ProcessEngine processEngine; @Before
public void init() {
//认证
securityUtil.logInAs("system");
} /**部署流程
说明:关于流程的部署,activiti7与springboot整合后,会自动部署已经创建好的流程文件(bpmn、png)
只要将流程定义文件存放在"resources/processes/下即可"
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-08-18 12:53:51
* @version 1.0
*/
@Test
public void repositoryProcess(){
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("holiday.bpmn")
.name("请假流程")
.deploy();
System.out.println(deployment.getName());
System.out.println(deployment.getDeploymentTime());
} /**分页查询系统中所有可用的流程定义
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-08-18 13:32:36
* @version 1.0
*/
@Test
public void findProcessList(){
Page<ProcessDefinition> processDefinitionPage = processRuntime
.processDefinitions(Pageable.of(0, 10));
List<ProcessDefinition> content = processDefinitionPage.getContent();
for (ProcessDefinition processDefinition : content) {
System.out.println(processDefinition.getName());
}
} /**启动流程
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-08-18 13:21:12
* @version 1.0
*/
@Test
public void startProcess(){ StartProcessPayload myProcess = ProcessPayloadBuilder
.start()
.withProcessDefinitionId("myProcess_1:1:5c5e0de3-e112-11ea-a73b-287fcf13e373")
.build(); myProcess.setProcessInstanceName("张三请假");
processRuntime.start(myProcess);
System.out.println("流程实例"+myProcess.getId()); } /**查询并完成任务
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-08-18 13:41:40
* @version 1.0
*/
@Test
public void findAndFinishTask() {
Page<Task> taskPage = taskRuntime.tasks(Pageable.of(0, 10)); if (taskPage.getTotalItems()>0){ List<Task> content = taskPage.getContent();
for (Task task : content) {
//拾取任务
taskRuntime.claim(new ClaimTaskPayloadBuilder()
.withTaskId(task.getId())
.build());
//完成任务
taskRuntime.complete(new CompleteTaskPayloadBuilder()
.withTaskId(task.getId())
.build());
}
} } }
End
本文仅介绍如何将activiti7集成到springboot中.具体的activiti使用请参考其他博文。
本文代码github地址:https://github.com/code81192/art-demo/tree/master/springboot-activiti7
springboot2整合activiti7具体步骤的更多相关文章
- Spring整合Hibernate的步骤
为什么要整合Hibernate?1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2.使用Spring管理Session对象 Hib ...
- SpringBoot2整合activiti6环境搭建
SpringBoot2整合activiti6环境搭建 依赖 <dependencies> <dependency> <groupId>org.springframe ...
- SpringBoot2 整合Kafka组件,应用案例和流程详解
本文源码:GitHub·点这里 || GitEE·点这里 一.搭建Kafka环境 1.下载解压 -- 下载 wget http://mirror.bit.edu.cn/apache/kafka/2.2 ...
- SpringBoot2 整合 Swagger2
SpringBoot2 整合 Swagger2 SpringBoot整合三板斧 第一步.引入pom <dependency> <groupId>com.spring4all&l ...
- SpringBoot2 整合 Swagger2文档 使用BootstrapUI页面
SpringBoot2 整合 Swagger2 SpringBoot整合三板斧 第一步.引入pom <dependency> <groupId>com.spring4all&l ...
- Spring-boot整合Activiti7
Spring-boot整合Activiti7 pom.xml <properties> <maven.compiler.source>15</mave ...
- SpringBoot2 整合Nacos组件,环境搭建和入门案例详解
本文源码:GitHub·点这里 || GitEE·点这里 一.Nacos基础简介 1.概念简介 Nacos 是构建以"服务"为中心的现代应用架构,如微服务范式.云原生范式等服务基础 ...
- Spring整合Hibernate详细步骤
阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...
- SpringBoot2 整合 Zookeeper组件,管理架构中服务协调
本文源码:GitHub·点这里 || GitEE·点这里 一.Zookeeper基础简介 1.概念简介 Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务.从设计模式角度来 ...
随机推荐
- anaconda一站式环境的搭建(anaconda、tensorflow、opencv)
搭建人工智能图像处理环境 Anaconda一站式开发环境搭建. 工欲善其事必先利其器,在我们学习之前,我们先要搭建一个属于我们自己的开发环境.我们开发的环境是有anaconda.testflow.op ...
- 32,初探c++标准库
1. 有趣的重载 (1)操作符<<:原义是按位左移,重载“<<”可将变量或常量左移到对象中 重载左移操作符(仿cout类) #include<stdio.h> co ...
- 保姆级教程,如何发现 GitHub 上的优质项目?
先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...
- SpringBoot学习之整合Druid的简单应用
一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...
- 线程_multiprocessing异步
from multiprocessing import Pool import time import os def test(): print("---进程池中的进程---pid=%d,p ...
- AJAX 是什么?
AJAX 简介 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX 是什么? AJAX = Asynchronous JavaScript and XML. AJAX ...
- Python 字典(Dictionary) cmp()方法
Python 字典(Dictionary) cmp()方法 描述 Python 字典的 cmp() 函数用于比较两个字典元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cm ...
- PHP trigger_error() 函数
定义和用法 trigger_error() 函数创建用户自定义的错误消息. trigger_error() 函数用于在用户指定的条件下触发一个错误消息.它可以与内建的错误处理程序一起使用,或者与由 s ...
- 4.2 省选模拟赛 流浪者 容斥dp
求出期望 所有情况很好搞 C(n+m-2,n-1). 也就是说求出所有情况的和乘以上面总方案的逆元即可. 可以发现所有情况和经过多少个障碍点有关 和所处位置无关. 简单的设f[i]表示从1,1到n,m ...
- 区块链钱包开发 - USDT - 三、实战(nodejs版本)
一.安装钱包 请参考另一篇随笔: 入口 二.获取测试usdt(TestOmni)步骤: 1.导入地址到钱包,往该地址充值测试比特币, 2.然后往 moneyqMan7uh8FqdCA2BV5yZ8qV ...