activiti整合spring

activiti的配置文件其实就是一份spring的配置文件,只是默认将processEngineConfiguration做为一个bean来读取。

当和spring进一步整合时,需要使用 SpringProcessEngineConfiguration(存在activiti-spring-5.10.jar) 类,其继承processEngineConfigurationImpl,activiti默认使用mybatis事务,使用 SpringProcessEngineConfiguration 时,必须指定 transactionManager 属性。

1. SpringProcessEngineConfiguration配置如下:

  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <!-- 连接属性-->
</bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 流程引擎的配置bean --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="databaseSchemaUpdate" value="drop-create" /> <property name="transactionManager" ref="transactionManager" /> <!-- 配置启动时加载的流程文件资源 --> <property name="deploymentResources"> <list> <value>/bpmn/EngineConfigurationTest.bpmn</value> <value>/bpmn/EngineConfigurationTest2.bpmn</value> </list> </property> </bean>

如果classpath下有多个文件,可以 

<property name="deploymentResources"  value="classpath:*:/bpmn/*.bpmn"/>

2. 接下来便可以通过 processEngineConfiguration bean 来得到流程引擎了:

    <!-- 流程引擎的bean -->
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>

 

3. activiti的各个服务组件(processEngineConfigurationImpl 创建),若由spring管理的话:

    <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="managementService" factory-bean="processEngine" factory-method="getManagementService" />

如果在业务service中使用这些组件,则直接ref注入即可。

4. 在activiti中使用spring的bean,则需要让activiti知道bean的存在

    <!-- 流程引擎的配置bean -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="databaseSchemaUpdate" value="drop-create" />
        <property name="transactionManager" ref="transactionManager" />
        <!-- 向processEngineConfiguration注入bean -->
        <property name="beans">
            <map>
                <entry key="myService" value-ref="myService" />
            </map>
        </property>
    </bean>

在activiti 流程文件中指定使用这个bean

    <process id="process1" name="process1">
        <startEvent id="startevent1" name="Start"></startEvent>
        <serviceTask id="servicetask1" name="Service Task"
            activiti:expression="#{myService.serviceMethod(name)}"></serviceTask>

表示使用myservice的serviceMethod方法,代码中可以传入参数启动流程

        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "activiti.use.bean.xml" });
        // 得到Activiti的服务组件
        RepositoryService repositoryService = (RepositoryService) ctx.getBean("repositoryService");
        RuntimeService runtimeService = (RuntimeService) ctx.getBean("runtimeService");
        // 部署流程文件
        repositoryService.createDeployment().addClasspathResource("bpmn/ActivitiUseBean.bpmn").deploy();
        // 初始化流程参数
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("name", "tom");
        // 启动流程
        runtimeService.startProcessInstanceByKey("process1", vars);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。