最近因公司的業務需求,需要使用工作流來做我們業務中的流程審批工作,so 就安排我做了這個工作,發現整合的時候有一些問題,及時的記錄下來分享給大家。
介紹:
一、如果你的web項目只是單純的web項目那么只需要將對應的jar包放到lib下構建一下即可
二、如果你的web項目是maven項目那么這里就需要在pom.xml中配置一下依賴包了,我們的項目就是maven項目所以這里重點介紹一下
1、在pom.xml <dependencies></dependencies>這個節點里面加入依賴文件
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.18.0</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.18.0</version> </dependency>
2、需要在src下加一個activiti-context.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- spring負責創建流程引擎的配置文件 --> <bean id="processEngineConfiguration" class="cn.com.sinosoft.workflow.service.MySpringProcessEngineConfiguration"> <!-- 數據源 --> <property name="dataSource1" ref="dataSource" /> <!-- 配置事務管理器,統一事務 --> <property name="transactionManager" ref="transactionManager" /> </bean> <!-- 創建流程引擎對象 --> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration" /> </bean> <!-- 相當於下面的代碼 RepositoryServicie repositoryService = processEngine.getRepositoryService(); RuntimeServicie repositoryService = processEngine.getRuntimeServicie(); TaskServicie taskServicie = processEngine.getTaskServicie(); HistoryServicie historyServicie = processEngine.getHistoryServicie(); --> <!-- 由流程引擎對象,提供的方法,創建項目中使用的Activiti工作流的Service --> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> <bean id="formService" factory-bean="processEngine" factory-method="getFormService" /> </beans>
3、需要在web.xml中加載生成bean的文件,也就是告訴項目要加載上面的activiti-context.xml文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext*.xml, classpath*:/applicationContext*.xml, /WEB-INF/applicationContext*.xml, classpath*:**/applicationContext*.xml, classpath:activiti-context.xml </param-value> </context-param>
4、以上配置成功的話其實就可以使用了,但是只要細心觀察的話就會發現一點在activiti-context.xml文件中配置"processEngineConfiguration" bean節點class路徑按照官網的demo應該是"org.activiti.spring.SpringProcessEngineConfiguration" 這個路徑,但是我可以明確的告訴你,如果你配置這個路徑會拋出一個異常,說DataSource注入不進去什么的,為此我特意檢查了一下這個類中的SetDataSource方法
@Override public ProcessEngineConfiguration setDataSource(DataSource dataSource) { if (dataSource instanceof TransactionAwareDataSourceProxy) { return super.setDataSource(dataSource); } else { // Wrap datasource in Transaction-aware proxy DataSource proxiedDataSource = new TransactionAwareDataSourceProxy(dataSource); return super.setDataSource(proxiedDataSource); } }
發現這個官方給的類中setDataSource方法竟然有返回值,那么我初步的想法就是可能是根據反射這塊出問題了,為此我就開始想對策,正常的set方法時沒有返回值的,也許可能就是因為這塊調用的是有返回值的方法所以出的錯誤,那么我就自己寫個類,去繼承這個官網給出的類,然后配置的時候調用我自己寫的類這樣就解決了我的問題,有了想法后開始實踐
package cn.com.sinosoft.workflow.service; import javax.sql.DataSource; public class MySpringProcessEngineConfiguration extends org.activiti.spring.SpringProcessEngineConfiguration{ public void setDataSource1(DataSource dataSource1) { super.setDataSource(dataSource1); } }
在activiti-context.xml中"processEngineConfiguration"這個bean配置了我的類后發現tomcat 竟然通過了,大功告成,這樣我就可以在我的項目里隨心所以的寫代碼啦!