Activiti-04-.Spring integration
ProcessEngineFactoryBean
<beanid="processEngineConfiguration"class="org.activiti.spring.SpringProcessEngineConfiguration">
...
</bean>
<beanid="processEngine"class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration"ref="processEngineConfiguration"/>
</bean>
Transactions
<beansxmlns="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">
<beanid="dataSource"class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<propertyname="driverClass"value="org.h2.Driver"/>
<propertyname="url"value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000"/>
<propertyname="username"value="sa"/>
<propertyname="password"value=""/>
</bean>
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<beanid="processEngineConfiguration"class="org.activiti.spring.SpringProcessEngineConfiguration">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="transactionManager"ref="transactionManager"/>
<propertyname="databaseSchemaUpdate"value="true"/>
<propertyname="jobExecutorActivate"value="false"/>
</bean>
<beanid="processEngine"class="org.activiti.spring.ProcessEngineFactoryBean">
<propertyname="processEngineConfiguration"ref="processEngineConfiguration"/>
</bean>
<beanid="repositoryService"factory-bean="processEngine"factory-method="getRepositoryService"/>
<beanid="runtimeService"factory-bean="processEngine"factory-method="getRuntimeService"/>
<beanid="taskService"factory-bean="processEngine"factory-method="getTaskService"/>
<beanid="historyService"factory-bean="processEngine"factory-method="getHistoryService"/>
<beanid="managementService"factory-bean="processEngine"factory-method="getManagementService"/>
...
<beans>
...
<tx:annotation-driventransaction-manager="transactionManager"/>
<beanid="userBean"class="org.activiti.spring.test.UserBean">
<propertyname="runtimeService"ref="runtimeService"/>
</bean>
<beanid="printer"class="org.activiti.spring.test.Printer"/>
</beans>
第一步: 构造Spring Context
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("org/activiti/examples/spring/SpringTransactionIntegrationTest-context.xml");
或者
@ContextConfiguration("classpath:org/activiti/spring/test/transaction/SpringTransactionIntegrationTest-context.xml")
第二步:获取service bean并调用它的方法
RepositoryService repositoryService =(RepositoryService) applicationContext.getBean("repositoryService");
String deploymentId = repositoryService
.createDeployment()
.addClasspathResource("org/activiti/spring/test/hello.bpmn20.xml")
.deploy()
.getId();
也可以以下面方式使用bean
UserBean userBean =(UserBean) applicationContext.getBean("userBean");
userBean.hello();
user bean的代码
publicclassUserBean{
/** injected by Spring */
privateRuntimeService runtimeService;
@Transactional
publicvoid hello(){
// here you can do transactional stuff in your domain model
// and it will be combined in the same transaction as
// the startProcessInstanceByKey to the Activiti RuntimeService
runtimeService.startProcessInstanceByKey("helloProcess");
}
publicvoid setRuntimeService(RuntimeService runtimeService){
this.runtimeService = runtimeService;
}
}
Expressions
Spring配置片段
<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" />
在流程定义文件中使用 spring.xml中的bean的方法的写法
<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"); } }
<beans ...> ... <bean id="printer" class="org.activiti.examples.spring.Printer" /> </beans>
Automatic resource deployment
集成Spring对部署资源也有特性,在流程引擎配置中,你可以指定一系列资源,当流程引擎呗创建时,所有的资源被扫描部署。有一个过滤器负责阻止多次部署,只有当资源呗改变时才会部署到Activiti DB中。这就意味着在多数用例中,Spring容器经常被重启. 如下例子:
<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>
下面是基于Spring的测试
@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()); } }
为了能够执行,还需要下面的
<bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule"> <property name="processEngine" ref="processEngine" /> </bean>
----------- 赠人玫瑰,手有余香 如果本文对您有所帮助,动动手指扫一扫哟 么么哒 -----------
未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负