一、与Spring集成
集成Spring配置
- 添加pom依赖activit-spring
- 基于Spring的默认配置activiti-context.xml
- Activiti核心服务注入Spring容器
基于Spring对Activiti管理
- 功能特征
- 集成Spring事物管理器
- 定义文件表达式中使用Spring bean
- 自动部署资源文件
- 功能特征
单元测试
- 添加pom依赖spring-test
- 辅助测试Rule:ActivitiRule
- 辅助测试TestCase:SpringActivitiTestCase
二、具体代码
添加依赖:注意版本,高版本会出现方法找不到的情况
xml<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
测试代码入口:
javapackage com.laogoubi.config; 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; /** * @ClassName ConfigMDCTest * @Description spring测试 * @Author eastern * @Date 2020/6/28 上午11:49 * @Version 1.0 **/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:activiti_context.cfg.xml"}) public class ConfigSpringTest { private static final Logger logger = LoggerFactory.getLogger(ConfigSpringTest.class); @Rule @Autowired public ActivitiRule activitiRule; @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Test @Deployment(resources = {"com/laogoubi/my-process_spring.bpmn20.xml"}) public void test() { ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process"); Task task = taskService.createTaskQuery().singleResult(); taskService.complete(task.getId()); } }
配置文件:activiti_context.cfg.xml
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"> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource"/> <property name="transactionManager" ref="transactionManager"/> <property name="databaseSchemaUpdate" value="true"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8&useSSL=false" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="initialSize" value="1"/> <property name="maxActive" value="10"/> <property name="filters" value="stat,slf4j"/> </bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"></property> </bean> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"></bean> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"></bean> <bean id="formService" factory-bean="processEngine" factory-method="getFormService"></bean> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"></bean> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"></bean> <bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule"> <property name="processEngine" ref="processEngine"></property> </bean> <!--注入自定义的bean--> <bean id="hellobean" class="com.laogoubi.delegate.HelloBean"></bean> </beans>
配置文件:my-process_spring.bpmn20.xml
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: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/test"> <process id="my-process"> <startEvent id="start" /> <sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" /> <userTask id="someTask" name="Activiti is awesome!" /> <sequenceFlow id="flow2" sourceRef="someTask" targetRef="hellobean" /> <!-- 加入自定义流程bean,表达式调用方法--> <serviceTask id="hellobean" activiti:expression="${hellobean.sayHello()}"></serviceTask> <sequenceFlow id="flow3" sourceRef="hellobean" targetRef="end" /> <endEvent id="end" /> </process> </definitions>
自定义bean:HelloBean
javapackage com.laogoubi.delegate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @ClassName HelloBean * @Description 测试 * @Author eastern * @Date 2020/7/1 下午4:48 * @Version 1.0 **/ public class HelloBean { private static final Logger logger = LoggerFactory.getLogger(HelloBean.class); public void sayHello(){ logger.info("hello"); } }