首页 > 代码库 > activiti整合spring
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);
activiti整合spring
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。