2017.2.21 activiti实战--第七章--Activiti与spring集成(一)配置文件

时间:2023-03-10 02:28:27
2017.2.21 activiti实战--第七章--Activiti与spring集成(一)配置文件

学习资料:《Activiti实战》

第七章 Activiti与容器集成

本章讲解activiti-spring可以做的事情,如何与现有系统集成,包含bean的注入、统一事务管理等。

7.1 流程引擎工厂

7.1.1 ProcessEngine

创建processEngine的方法有三种:

 通过配置文件
测试中通过ActivitiRule
通过ProcessEngines类

7.1.2 ProcessEngineFactory

与spring集成的目的有两个:

 通过spring统一管理ProcessEngine和七大Service的获取
业务与流程引擎的统一事务管理

(1)pom.xml

将activiti与spring集成,先要在pom.xml中增加依赖activiti-spring:(此处忽略了spring本身的依赖,请自行配置)

         <!-- activit begin -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.16.3</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.16.3</version>
</dependency>
<!-- activit end -->

(2)配置文件applicationContext-test.xml

applicationContext.xml分为几个部分:数据源、事务管理器、引擎配置、引擎工厂、获取service。(同样,spring自身的一些配置此处略过。)

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
</bean> <bean id="processEngineFactory" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean> <bean id="repositoryService" factory-bean="processEngineFactory" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngineFactory" factory-method="getRuntimeService" />
<bean id="formService" factory-bean="processEngineFactory" factory-method="getFormService" />
<bean id="identityService" factory-bean="processEngineFactory" factory-method="getIdentityService" />
<bean id="taskService" factory-bean="processEngineFactory" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngineFactory" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngineFactory" factory-method="getManagementService" />
</beans>

(3)ProcessEngine和xxxService使用示例

在配置文件applicationContext-test.xml文件中加上一段:

     <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="me.kafeitu.activiti">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

然后在代码中使用:

 @Autowired
RuntimeService runtimeService;

7.2 自动部署流程定义

(1)场景

概念:通过Spring创建 ProcessEngine 时一个独有的功能。可以在初始化 ProcessEngine 时自动将定义的资源部署到 ProcessEngine 中。

问题:每次启动都会加载spring配置文件,并且随之初始化ProcessEngine,那么是不是每次都会部署一遍呢?那么一旦重启部署版本就变成新的了?(activiti中有部署版本的参数)Activiti处理整个的办法是,只有流程数据库中没有和自动部署的流程定义相同的记录才会部署,也就是没有新的流程定义就不会再部署一遍。

优点:对新系统的上线或者开发过程中加入新的流程非常有用,可以自动部署引擎数据库中不存在的,或者修改过的流程定义。

(2)配置

在引擎工程的属性里加上一个部署资源的属性。

1     <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
2 <property name="dataSource" ref="dataSource" />
3 <property name="transactionManager" ref="transactionManager" />
4 <property name="databaseSchemaUpdate" value="true" />
5 <property name="jobExecutorActivate" value="false" />
6 <property name="deploymentResource" value="classpath*:/chapter6/**/*.bpmn"/>
7 </bean>

(3)代码测试

     long count = repositoryService.createProcessDefinitionQuery().count();
assertEquals(1,count);