美文网首页
Activiti流程引擎配置7-Activiti与Spring集

Activiti流程引擎配置7-Activiti与Spring集

作者: 安安汐而 | 来源:发表于2019-08-20 15:44 被阅读0次

    Demo项目代码:
    https://gitee.com/AnAnXiEr/activiti6-demo

    1、集成Spring配置
    2、基于Spring对Activiti管理
    3、基于Spring的流程单元测试

    1、相关配置

    添加pom依赖activiti-spring
    基于Spring的默认配置activiti-context.xml
    Activiti核心服务注入Spring容器

    2、功能特征

    集成Spring事务管理器
    定义文件表达式中使用Spring bean
    自动部署资源文件

    3、单元测试

    添加pom依赖spring-test
    辅助测试Rule:ActivitiRule
    辅助测试TestCase:SpringActivitiTestCase

    代码

    activiti-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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">
    
        <!--dataSoucre-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="jdbc:h2:mem:activiti"/>
            <property name="driverClassName" value="org.h2.Driver"/>
            <property name="username" value="sa"/>
            <property name="password" value=""/>
    
            <property name="filters" value="stat,slf4j"/>
        </bean>
    
        <!--事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--以druid数据源连接-->
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!--修改为SpringProcessEngineConfiguration-->
        <bean id="processEngineConfiguration"
              class="org.activiti.spring.SpringProcessEngineConfiguration">
    
            <!--需要单独创建一个数据库、spring需要对事务进行控制-->
            <!--以druid数据源连接-->
            <property name="dataSource" ref="dataSource"/>
            <!--事务管理器-->
            <property name="transactionManager" ref="transactionManager"/>
    
            <property name="databaseSchemaUpdate" value="true"/>
        </bean>
    
    
        <!--流程引擎对象-->
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
            <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
        </bean>
    
        <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
        <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
        <bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>
        <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
        <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    
        <bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">
            <property name="processEngine" ref="processEngine"/>
        </bean>
    
        <bean id="helloBean" class="com.imooc.activiti.activitidemo.delegage.HelloBean"/>
    
    </beans>
    

    spring-process.bpmn20.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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"
                 typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
                 targetNamespace="http://www.activiti.org/processdef">
        <process id="my-process" name="二级审批流程" isExecutable="true">
            <documentation>MDC test process</documentation>
    
            <startEvent id="startEvent" name="开始"/>
    
            <sequenceFlow id="sid-905AF026-9221-4C80-AF9A-29C581E59503" sourceRef="startEvent"
                          targetRef="tlApprove"/>
    
            <userTask id="tlApprove" name="主管审批"/>
    
            <sequenceFlow id="flow2" sourceRef="tlApprove" targetRef="hellobean"/>
    
            <serviceTask id="hellobean" activiti:expression="${helloBean.sayHello()}}"/>
    
            <sequenceFlow id="sid-905AF026-9221-4C80-AF9A-29C581E59505" sourceRef="hellobean" targetRef="endEventCancel"/>
    
            <endEvent id="endEventCancel" name="取消"/>
        </process>
    </definitions>
    

    ConfigSpringTest

    package com.imooc.activiti.activitidemo;
    
    import org.activiti.engine.RuntimeService;
    import org.activiti.engine.TaskService;
    import org.activiti.engine.runtime.ProcessInstance;
    import org.activiti.engine.task.Task;
    import org.activiti.engine.test.ActivitiRule;
    import org.activiti.engine.test.Deployment;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import static junit.framework.TestCase.assertEquals;
    import static junit.framework.TestCase.assertNotNull;
    
    /**
     * @Description Spring测试
     * @Author 胡浩
     * @Date 2019/8/16
     **/
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:activiti-context.xml"})
    public class ConfigSpringTest {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ConfigSpringTest.class);
    
        @Rule
        @Autowired
        //自动注入进来、取activiti-context里面的配置
        public ActivitiRule activitiRule;
    //    public ActivitiRule activitiRule = new ActivitiRule("activiti-context.xml");
    
        @Autowired
        private RuntimeService runtimeService;
    
        @Autowired
        private TaskService taskService;
    
    
        @Test
        @Deployment(resources = "spring-process.bpmn20.xml")
        public void testConfig() {
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
            Task task = taskService.createTaskQuery().singleResult();
            taskService.complete(task.getId());
        }
    }
    
    

    HelloBean

    package com.imooc.activiti.activitidemo.delegage;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * @Description
     * @Author 胡浩
     * @Date 2019/8/20
     **/
    public class HelloBean {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(HelloBean.class);
    
        public void sayHello() {
            LOGGER.info("== sayHello ==");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Activiti流程引擎配置7-Activiti与Spring集

          本文链接:https://www.haomeiwen.com/subject/znupsctx.html