Spring和activiti進(jìn)行整合過(guò)程解析
一、整合原理
activiti的配置文件本身就是一個(gè)spring的配置文件,但默認(rèn)情況下只講ProcessEngineConfiguration作為一個(gè)bean來(lái)使用,調(diào)用ProcessEngines.getDefaultProcessEngine()加載的就是配置文件中的這個(gè)bean。和spring整合后就可以把bean的管理讓spring來(lái)進(jìn)行,在代碼中獲取任意的bean。
activiti提供了一個(gè)spring模塊,在一個(gè)spring工程中引入這個(gè)模塊就能夠整合
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.22.0</version> </dependency>
并且引入這個(gè)依賴后就不需要再單獨(dú)引入activiti的依賴了,這里邊已經(jīng)包含了
二、整合步驟
2.1 新建一個(gè)maven工程并導(dǎo)入相關(guān)依賴
<dependencies>
<!--spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
這里導(dǎo)入了spring,activiti,數(shù)據(jù)庫(kù)驅(qū)動(dòng)等依賴。
2.2 創(chuàng)建spring配置文件
在resources目錄下創(chuàng)建spring配置文件,applicationContext.xml,其中主要配置如下bean
(1)數(shù)據(jù)源(連接池)
(2)事務(wù)管理器(和spring整合后必須配置事務(wù)管理器)
(3)流程引擎配置對(duì)象,這里可以注入一些流程引擎的配置
(4)流程引擎對(duì)象
(5)activiti的服務(wù)組件,配置后在程序中可以直接從容器中獲取
完整的配置文件如下
<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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置連接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti_01"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置使用spring提供的的流程引擎配置對(duì)象 -->
<bean id="processEngineConfiguration"
class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="databaseSchemaUpdate" value="true"></property>
<property name="deploymentResources" value="classpath*:/process/*.bpmn"></property>
</bean>
<!-- 配置流程引擎工廠bean,獲取這個(gè)bean就可以直接獲取到流程引擎對(duì)象 -->
<!-- 注意這個(gè)不是使用靜態(tài)工廠來(lái)獲取bean -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
</bean>
<!-- 配置activiti的服務(wù)組件 -->
<!-- 這里是通過(guò)實(shí)例工廠來(lái)創(chuàng)建服務(wù)組件的對(duì)象
ProcessEngine類的父類EngineServices中有獲取服務(wù)組件的get方法,所以這里把processEngine當(dāng)做實(shí)例工廠來(lái)使用,
而這幾個(gè)對(duì)象是在創(chuàng)建流程引擎配置對(duì)象時(shí)就new出來(lái)的
-->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService">
</bean>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService">
</bean>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService">
</bean>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService">
</bean>
</beans>
三、測(cè)試
在代碼中加載spring配置文件,并直接從容器里獲取服務(wù)組件,看能否直接使用服務(wù)組件
@Test
public void test4() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通過(guò)容器獲取taskService組件
TaskService taskService= (TaskService) context.getBean("taskService");
System.out.println(taskService);
List<Task> list = taskService.createTaskQuery().taskAssignee("tom").list();
for (Task task : list) {
System.out.println(task);
}
}
如果能夠成功運(yùn)行,說(shuō)明activiti和spring已經(jīng)整合完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級(jí)實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級(jí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧<BR>2022-11-11
springcloud結(jié)合bytetcc實(shí)現(xiàn)數(shù)據(jù)強(qiáng)一致性原理解析
這篇文章主要介紹了springcloud結(jié)合bytetcc實(shí)現(xiàn)數(shù)據(jù)強(qiáng)一致性原理解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件本地窗口實(shí)現(xiàn)(5)
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件,F(xiàn)TP軟件本地窗口的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Spring Boot集成Redis實(shí)戰(zhàn)操作功能
這篇文章主要介紹了Spring Boot集成Redis實(shí)戰(zhàn)操作,包括如何集成redis以及redis的一些優(yōu)點(diǎn),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
java system類使用方法示例 獲取系統(tǒng)信息
這篇文章主要介紹了java system類使用方法,該類中的方法都是靜態(tài)的。不能被實(shí)例化,沒有對(duì)外提供構(gòu)造函數(shù),該類可以獲取系統(tǒng)信息2014-01-01
Java利用Map實(shí)現(xiàn)計(jì)算文本中字符個(gè)數(shù)
這篇文章主要為大家詳細(xì)介紹了Java如何利用Map集合實(shí)現(xiàn)計(jì)算文本中字符個(gè)數(shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08

