Activiti工作流框架学习六:与Spring框架整合

一、Java项目

导入activiti提供的示例rest里的44个jar包,注意:还需要再导入mysql-connector-java-5.1.5-bin.jar


<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="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///activiti_day2" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="transactionManager" ref="transactionManager"/>
        <property name="databaseSchemaUpdate" value="true" />
    bean>

    
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    bean>
beans>
//示例代码
public class SpringActivitiTest {
    public static void main(String[] args) {
        // 创建spring工厂
        ApplicationContext ctx = new ClassPathXmlApplicationContext("com\\zyj\\activiti\\spring\\applicationContext.xml");
        // 从工厂中获得对象
        ProcessEngine processEngine = (ProcessEngine) ctx.getBean("processEngine");
        List list = processEngine.getRepositoryService().createProcessDefinitionQuery().list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println(processDefinition);
        }
    }
}

二、与SSH整合的Web项目



<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${driverClass}"/>
    <property name="jdbcUrl" value="${jdbcUrl}"/>
    <property name="user" value="${user}"/>
    <property name="password" value="${password}"/>
bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    
    <property name="dataSource" ref="dataSource"/>
    
    <property name="transactionManager" ref="transactionManager"/>
    <property name="databaseSchemaUpdate" value="true" />
bean>


<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
bean>


<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="identityService" factory-bean="processEngine" factory-method="getIdentityService">bean>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService">bean>

/**
 * 流程定义管理Action
 */
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport {
    // 注入Service
    @Autowired
    private RepositoryService repositoryService;
    /**
     * 查询最新版本流程定义列表数据
     */
    public String list() {
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        query.latestVersion();// 查询最新版本
        query.orderByProcessDefinitionName().desc();// 排序
        List list = query.list();// 执行查询
        // 压栈
        ActionContext.getContext().getValueStack().set("list", list);
        return "list";
    }
    // 接收上传的文件,包含.bpmn和.png的.zip文件
    private File zipFile;
    public void setZipFile(File zipFile) {
        this.zipFile = zipFile;
    }
    /**
     * 部署流程定义
     * @throws FileNotFoundException 
     */
    public String deploy() throws FileNotFoundException {
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        deploymentBuilder.addZipInputStream(new ZipInputStream(new FileInputStream(zipFile)));
        deploymentBuilder.deploy();
        return "toList";
    }
    //接收流程定义id
    private String id;
    public void setId(String id) {
        this.id = id;
    }
    /**
     * 输出png图片,用于页面展示流程定义图
     */
    public String showpng(){
        //获取png图片对应的输入流
        InputStream pngStream = repositoryService.getProcessDiagram(id);
        ActionContext.getContext().getValueStack().set("pngStream", pngStream);
        return "showpng";
    }
    /**
     * 删除流程定义
     */
    public String delete(){
        String deltag = "0";
        //根据流程定义id查询部署id
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        query.processDefinitionId(id);//根据id过滤
        ProcessDefinition processDefinition = query.singleResult();
        String deploymentId = processDefinition.getDeploymentId();
        try{
            repositoryService.deleteDeployment(deploymentId);
        }catch (Exception e) {
            //当前要删除的流程定义正在使用
            deltag = "1";
            ActionContext.getContext().getValueStack().set("deltag", deltag);
            ProcessDefinitionQuery query2 = repositoryService.createProcessDefinitionQuery();
            query2.latestVersion();// 查询最新版本
            query2.orderByProcessDefinitionName().desc();// 排序
            List list = query2.list();// 执行查询
            // 压栈
            ActionContext.getContext().getValueStack().set("list", list);
            return "list";
        }
        return "toList";
    }
}

struts.xml


<action name="processDefinitionAction_*" class="processDefinitionAction"
    method="{1}">
    <result name="list">/WEB-INF/pages/workflow/processdefinition_list.jsp
    result>
    <result name="toList" type="redirectAction">
        processDefinitionAction_list
    result>
    <result name="showpng" type="stream">
        <param name="contentType">image/pngparam>
        <param name="inputName">pngStreamparam>
        
    result>
action>

你可能感兴趣的:(【框架】Activiti)