2017.2.21 activiti實戰--第七章--Activiti與spring集成(一)配置文件


學習資料:《Activiti實戰》

 

第七章 Activiti與容器集成

本章講解activiti-spring可以做的事情,如何與現有系統集成,包含bean的注入、統一事務管理等。

7.1 流程引擎工廠

7.1.1 ProcessEngine

創建processEngine的方法有三種:

1 通過配置文件
2 測試中通過ActivitiRule
3 通過ProcessEngines類

 

7.1.2 ProcessEngineFactory

與spring集成的目的有兩個:

1 通過spring統一管理ProcessEngine和七大Service的獲取
2 業務與流程引擎的統一事務管理

 

(1)pom.xml

將activiti與spring集成,先要在pom.xml中增加依賴activiti-spring:(此處忽略了spring本身的依賴,請自行配置)

 1         <!-- activit begin -->
 2         <dependency>
 3             <groupId>org.activiti</groupId>
 4             <artifactId>activiti-engine</artifactId>
 5             <version>5.16.3</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.activiti</groupId>
 9             <artifactId>activiti-spring</artifactId>
10             <version>5.16.3</version>
11         </dependency>
12         <!-- activit end -->

 

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

applicationContext.xml分為幾個部分:數據源、事務管理器、引擎配置、引擎工廠、獲取service。(同樣,spring自身的一些配置此處略過。)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
 3     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd
 5                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
 6 
 7     <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 8         <property name="driverClass" value="org.h2.Driver" />
 9         <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
10         <property name="username" value="sa" />
11         <property name="password" value="" />
12     </bean>
13 
14     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
15         <property name="dataSource" ref="dataSource" />
16     </bean>
17     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
18 
19     <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
20         <property name="dataSource" ref="dataSource" />
21         <property name="transactionManager" ref="transactionManager" />
22         <property name="databaseSchemaUpdate" value="true" />
23         <property name="jobExecutorActivate" value="false" />
24     </bean>
25 
26     <bean id="processEngineFactory" class="org.activiti.spring.ProcessEngineFactoryBean">
27         <property name="processEngineConfiguration" ref="processEngineConfiguration" />
28     </bean>
29 
30     <bean id="repositoryService" factory-bean="processEngineFactory" factory-method="getRepositoryService" />
31     <bean id="runtimeService" factory-bean="processEngineFactory" factory-method="getRuntimeService" />
32     <bean id="formService" factory-bean="processEngineFactory" factory-method="getFormService" />
33     <bean id="identityService" factory-bean="processEngineFactory" factory-method="getIdentityService" />
34     <bean id="taskService" factory-bean="processEngineFactory" factory-method="getTaskService" />
35     <bean id="historyService" factory-bean="processEngineFactory" factory-method="getHistoryService" />
36     <bean id="managementService" factory-bean="processEngineFactory" factory-method="getManagementService" />
37 </beans>

 

(3)ProcessEngine和xxxService使用示例

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

1     <!-- 使用annotation 自動注冊bean, 並保證@Required、@Autowired的屬性被注入 -->
2     <context:component-scan base-package="me.kafeitu.activiti">
3         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
4     </context:component-scan>

 

然后在代碼中使用:

1 @Autowired 2 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)代碼測試

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM