欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年03月09日 08:54:56   作者:程序曉猿  
這篇文章主要介紹了Spring和activiti進(jìn)行整合過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、整合原理

activiti的配置文件本身就是一個(gè)spring的配置文件,但默認(rèn)情況下只講ProcessEngineConfiguration作為一個(gè)bean來使用,調(diào)用ProcessEngines.getDefaultProcessEngine()加載的就是配置文件中的這個(gè)bean。和spring整合后就可以把bean的管理讓spring來進(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ù)庫驅(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)工廠來獲取bean -->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
  </bean>
  
  <!-- 配置activiti的服務(wù)組件 -->
  <!-- 這里是通過實(shí)例工廠來創(chuàng)建服務(wù)組件的對(duì)象
  ProcessEngine類的父類EngineServices中有獲取服務(wù)組件的get方法,所以這里把processEngine當(dāng)做實(shí)例工廠來使用,
  而這幾個(gè)對(duì)象是在創(chuàng)建流程引擎配置對(duì)象時(shí)就new出來的
   -->
  <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");
  //通過容器獲取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)行,說明activiti和spring已經(jīng)整合完成了。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論