工作流Activiti的学习总结(四)Spring和Activiti的整合配置讲解

SpringActiviti的整合:

SpringActiviti的整合中ProcessEngineFactoryBean成为了两者的整合点。ProcessEngineFactoryBeanorg.activiti.spring.ProcessEngineFactoryBean。提供了ProcessEngine的配置和创建的功能。
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

    ...

</bean>

  

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

  <property name="processEngineConfiguration" ref="processEngineConfiguration" />

</bean>

如果在包含事物的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">

 <!-- 采用spring的数据源类创建一个数据源 -->

  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">

    <property name="driverClass" value="org.h2.Driver" />

    <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />

    <property name="username" value="sa" />

    <property name="password" value="" />

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

    <!-- 设置数据库schema的更新方式 -->

    <property name="databaseSchemaUpdate" value="true" />

    <!-- 是否启动jobExecutor -->

    <property name="jobExecutorActivate" value="false" />

  </bean>

   <!-- 创建一个流程引擎bean -->

  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

    <property name="processEngineConfiguration" ref="processEngineConfiguration" />

  </bean>

  <!-- 创建activiti提供的各种服务 -->

  <!-- 工作流仓储服务 -->

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

  <!-- 工作流唯一服务 -->

  <bean id="IdentityService" factory-bean="processEngine" factory-method="getIdentityService" />

 

...

工作流中Expressions的使用
  
在使用springSpringProcessEngineConfiguration时,如果没有beans属性表示所有bean都可以暴露给activiti的流程文件xml访问。如果配置beans但是没有配置mapbean信息,则没有暴露的bean。如果map中配置部分bean表示只是暴露部分beanactiviti使用。
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

  ...

  <property name="beans">

    <map>

      <entry key="printer" value-ref="printer" />

    </map>

  </property>

</bean>

  

  <bean id="printer" class="org.activiti.examples.spring.Printer" />

暴露的bean可以在activiti的流程xml中使用。如下调用bean的方法
<bean id="printer" class="org.activiti.examples.spring.Printer" />
<definitions id="definitions" ...>

  

  <process id="helloProcess">

  

    <startEvent id="start" />

    <sequenceFlow id="flow1" sourceRef="start" targetRef="print" />

    

    <serviceTask id="print" activiti:expression="#{printer.printMessage()}" />

    <sequenceFlow id="flow2" sourceRef="print" targetRef="end" />

    

    <endEvent id="end" />

    

  </process>

 

</definitions>
public class Printer {

 

  public void printMessage() {

    System.out.println("hello world");

  }

}

自动资源部署(Automatic resource deploy
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

  ...

  <property name="deploymentResources" value="classpath*:/org/activiti/spring/test/autodeployment/autodeploy.*.bpmn20.xml" />

</bean>

  

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

  <property name="processEngineConfiguration" ref="processEngineConfiguration" />

</bean>

Springactivit的整合测试
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:org/activiti/spring/test/junit4/springTypicalUsageTest-context.xml")

public class MyBusinessProcessTest {

  

  @Autowired

  private RuntimeService runtimeService;

  

  @Autowired

  private TaskService taskService;

  

  @Autowired

  @Rule

  public ActivitiRule activitiSpringRule;

  

  @Test

  @Deployment

  public void simpleProcessTest() {

    runtimeService.startProcessInstanceByKey("simpleProcess");

    Task task = taskService.createTaskQuery().singleResult();

    assertEquals("My Task", task.getName());

  

    taskService.complete(task.getId());

    assertEquals(0, runtimeService.createProcessInstanceQuery().count());

   

  }

}      

当在spring的配置中配置ActivitiRule时候,测试bean将自动被注入:

<bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">

  <property name="processEngine" ref="processEngine" />

</bean>  

你可能感兴趣的:(Activiti)