项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可。现在来给项目添加一个Activiti5.22支持,Springboot采用2.0.7的版本(后来试着升级到2.1.1和2.2.1,都是失败),具体内容如下:

  1. pom.xml添加以下配置信息


    org.springframework.boot
    spring-boot-starter-parent
    2.0.7.RELEASE
    


    UTF-8
    UTF-8
    1.8
    
    -Dfile.encoding=UTF-8
    
    5.22.0   


    
        org.activiti
        activiti-spring-boot-starter-basic
        ${activiti.version}
    
  1. 配置文件中添加以下配置信息
# 设置系统启动时自动建立Activiti表(Springboot1.x的版本中不需要做此设置)
# always为始终执行初始化,embedded只初始化内存数据库(默认值),如h2等,never为不执行初始化.
spring.datasource.initialization-mode=always

# Activiti配置
## 自动构建Activiti数据库表,设置value="true",启动时检测,无表创建,有表跳过;设置value="drop-create"
## 每次启动时建新表(Springboot1.x中生效,Springboot2.x中已经不生效,因此关闭)
#spring.activiti.database-schema-update=true
## Activiti自动部署验证设置,:true-开启(默认)、false-关闭
spring.activiti.check-process-definitions=false
## 设置true后将代替那些老的Job executor
spring.activiti.async-executor-enabled=false
## Activiti定时扫描任务,默认:true(开启),不使用的话建议关掉,需要和spring.activiti.async-executor-enabled=false配合使用,否则不起作用
spring.activiti.job-executor-activate=false

3.创建流程文件
  将创建好的文件放置在src/main/resources目录下
SpringBoot入门十四,添加Activiti5.22工作流_第1张图片

4.创建一个测试service


import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivitiTestService {
    @Autowired  
    private RepositoryService repositoryService; 
    @Autowired  
    private RuntimeService runtimeService;  
    @Autowired  
    private TaskService taskService; 

    /**
     * @功能描述:   流程测试
     *
     * @param flowType 
     */
    public void test(String flowType){
        //部署流程,只要是符合BPMN2规范的XML文件,理论上都可以被ACTIVITI部署
        Deployment deployment = repositoryService.createDeployment().name("测试流程一").addClasspathResource("testProcess.bpmn").addClasspathResource("testProcess.png").deploy();
        //获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        //启动流程定义,返回流程实例
        ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("流程创建成功,当前流程实例ID:"+processId);

        Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("第一次执行前,任务名称:"+task.getName());
        taskService.complete(task.getId());

        int count = 2;
        while(true){
            task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
            if (null == task) {
                System.out.println("task为null,任务执行完毕:"+task);
                break;
            } else {
                System.out.println("第"+count+++"次执行前,任务名称:"+task.getName());
                taskService.complete(task.getId());
            }
        }
    }
}

5.创建一个测试controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qfx.common.bean.MessageBean;
import com.qfx.common.controller.BaseController;
import com.qfx.workflow.service.ActivitiTestService;
import com.qfx.workflow.service.ModelService;

@Controller
@RequestMapping("activiti")
public class ActivitiController extends BaseController {

    @Autowired
    ActivitiTestService activitiService;

    @Autowired
    ModelService modelService;

    @RequestMapping("test")
    public String test(String flowType){
        // 流程测试
        activitiService.test(flowType);

        MessageBean messageBean = new MessageBean();
        messageBean.setMessage("测试流程执行完毕了,请看后台输出!");
        request.setAttribute("messageBean", messageBean);

        return "../index";
    }
}

6.测试调用,控制台显示调用成功
SpringBoot入门十四,添加Activiti5.22工作流