Spring和activiti进行整合过程解析

所属分类: 软件编程 / java 阅读数: 80
收藏 0 赞 0 分享

一、整合原理

activiti的配置文件本身就是一个spring的配置文件,但默认情况下只讲ProcessEngineConfiguration作为一个bean来使用,调用ProcessEngines.getDefaultProcessEngine()加载的就是配置文件中的这个bean。和spring整合后就可以把bean的管理让spring来进行,在代码中获取任意的bean。

activiti提供了一个spring模块,在一个spring工程中引入这个模块就能够整合

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

并且引入这个依赖后就不需要再单独引入activiti的依赖了,这里边已经包含了

二、整合步骤

2.1 新建一个maven工程并导入相关依赖

<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>

这里导入了spring,activiti,数据库驱动等依赖。

2.2 创建spring配置文件

在resources目录下创建spring配置文件,applicationContext.xml,其中主要配置如下bean

(1)数据源(连接池)

(2)事务管理器(和spring整合后必须配置事务管理器)

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

(4)流程引擎对象

(5)activiti的服务组件,配置后在程序中可以直接从容器中获取

完整的配置文件如下

<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>
  
  <!-- 事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  
  <!-- 配置使用spring提供的的流程引擎配置对象 -->
  <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,获取这个bean就可以直接获取到流程引擎对象 -->
  <!-- 注意这个不是使用静态工厂来获取bean -->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
  </bean>
  
  <!-- 配置activiti的服务组件 -->
  <!-- 这里是通过实例工厂来创建服务组件的对象
  ProcessEngine类的父类EngineServices中有获取服务组件的get方法,所以这里把processEngine当做实例工厂来使用,
  而这几个对象是在创建流程引擎配置对象时就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>

三、测试

在代码中加载spring配置文件,并直接从容器里获取服务组件,看能否直接使用服务组件

@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);
  }
}

如果能够成功运行,说明activiti和spring已经整合完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多