activiti和SSH项目做整合

最近因公司的业务需求,需要使用工作流来做我们业务中的流程审批工作,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 竟然通过了,大功告成,这样我就可以在我的项目里随心所以的写代码啦!

原文地址:https://www.cnblogs.com/mycifeng/p/4952152.html