工作流activiti 6 学习笔记十五 java 应用整合spring

举报
lu_zhishen 发表于 2021/10/21 17:37:14 2021/10/21
【摘要】 配置spring.xml 文件,可以随意命名<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org...

配置spring.xml 文件,可以随意命名

<beans xmlns="http://www.springframework.org/schema/beans"
       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">
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/myactlearn" />
	    <property name="driverClass" value="com.mysql.jdbc.Driver" />
	    <property name="username" value="root" />
	    <property name="password" value="Happy123" />
	</bean>
	
	<!-- 配置事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置流程引擎 -->
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
	<property name="dataSource" ref="dataSource"></property>
    <property name="databaseSchemaUpdate" value="true" />
    <property name="transactionManager" ref="transactionManager"></property>
    <property name="asyncExecutorActivate" value="true" />
  </bean>
  <!-- 初始化引擎 -->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
  	<property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
  </bean>
  
  <!-- 初始化常用的几个服务 -->
  <bean id="rs" factory-bean="processEngine" factory-method="getRepositoryService"></bean>
  <bean id="runS" factory-bean="processEngine" factory-method="getRuntimeService"></bean>
  <bean id="ts" factory-bean="processEngine" factory-method="getTaskService"></bean>

</beans>

测试类:

package org.scf.act.spring;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 结合sping并部署一个流程
 * @author scf
 *
 */
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"activiti.cfg.spring.xml"}) ;
		ProcessEngine engine = (ProcessEngine)ctx.getBean("processEngine");
		
		// 存储服务
		RepositoryService rs =(RepositoryService)ctx.getBean("rs");
		// 运行时服务
		RuntimeService runS = (RuntimeService)ctx.getBean("runS");
		// 任务服务
		TaskService ts = (TaskService)ctx.getBean("ts");
		// 部署流程
		Deployment deploye = rs.createDeployment().addClasspathResource("multi.bpmn").deploy();
		ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
		
		ProcessInstance  pi = runS.startProcessInstanceById(pdf.getId());
		System.out.println(pi.getId());

		
	}
}


代码解释:

加载spring文件,从spring文件中获取bean,根据spring factroy bean 和method 获取服务对象。

注:

也可以在spring配置文件中直接部署流程文件

代码如下:

<!-- 配置流程引擎 -->
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
	<property name="dataSource" ref="dataSource"></property>
    <property name="databaseSchemaUpdate" value="true" />
    <property name="transactionManager" ref="transactionManager"></property>
    <property name="asyncExecutorActivate" value="true" />
    <property name="deploymentResources">
    	<list>
    		<value>leave.bpmn</value>
    	</list>
    </property>
  </bean>
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。