久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring和activiti進(jìn)行整合過程解析

瀏覽:81日期:2023-09-17 16:13:12

一、整合原理

activiti的配置文件本身就是一個spring的配置文件,但默認(rèn)情況下只講ProcessEngineConfiguration作為一個bean來使用,調(diào)用ProcessEngines.getDefaultProcessEngine()加載的就是配置文件中的這個bean。和spring整合后就可以把bean的管理讓spring來進(jìn)行,在代碼中獲取任意的bean。

activiti提供了一個spring模塊,在一個spring工程中引入這個模塊就能夠整合

<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.22.0</version></dependency>

并且引入這個依賴后就不需要再單獨引入activiti的依賴了,這里邊已經(jīng)包含了

二、整合步驟

2.1 新建一個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ù)庫驅(qū)動等依賴。

2.2 創(chuàng)建spring配置文件

在resources目錄下創(chuàng)建spring配置文件,applicationContext.xml,其中主要配置如下bean

(1)數(shù)據(jù)源(連接池)

(2)事務(wù)管理器(和spring整合后必須配置事務(wù)管理器)

(3)流程引擎配置對象,這里可以注入一些流程引擎的配置

(4)流程引擎對象

(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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd'> <!-- 配置連接池 --> <bean 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 class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='dataSource'></property> </bean> <!-- 配置使用spring提供的的流程引擎配置對象 --> <bean 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,獲取這個bean就可以直接獲取到流程引擎對象 --> <!-- 注意這個不是使用靜態(tài)工廠來獲取bean --> <bean class='org.activiti.spring.ProcessEngineFactoryBean'> <property name='processEngineConfiguration' ref='processEngineConfiguration'></property> </bean> <!-- 配置activiti的服務(wù)組件 --> <!-- 這里是通過實例工廠來創(chuàng)建服務(wù)組件的對象 ProcessEngine類的父類EngineServices中有獲取服務(wù)組件的get方法,所以這里把processEngine當(dāng)做實例工廠來使用, 而這幾個對象是在創(chuàng)建流程引擎配置對象時就new出來的 --> <bean factory-bean='processEngine' factory-method='getRepositoryService'> </bean> <bean factory-bean='processEngine' factory-method='getRuntimeService'> </bean> <bean factory-bean='processEngine' factory-method='getTaskService'> </bean> <bean factory-bean='processEngine' factory-method='getHistoryService'> </bean></beans>

三、測試

在代碼中加載spring配置文件,并直接從容器里獲取服務(wù)組件,看能否直接使用服務(wù)組件

@Testpublic void test4() { ApplicationContext context = new ClassPathXmlApplicationContext('applicationContext.xml'); //通過容器獲取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); }}

如果能夠成功運行,說明activiti和spring已經(jīng)整合完成了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 老河口市| 永胜县| 三原县| 河池市| 黄陵县| 四平市| 黎川县| 从化市| 盖州市| 崇文区| 宁远县| 香港| 辉南县| 泾川县| 勃利县| 彝良县| 利辛县| 朝阳县| 巨野县| 轮台县| 平安县| 电白县| 金堂县| 延安市| 天津市| 隆林| 林州市| 伊金霍洛旗| 个旧市| 合江县| 商河县| 金湖县| 浑源县| 泰州市| 桦川县| 淳化县| 闽清县| 阜新市| 湖南省| 成安县| 寿光市|