Activiti获取ProcessEngine的三种方法

本文介绍了三种初始化Activiti流程引擎的方法:直接通过ProcessEngineConfiguration配置、通过XML配置文件加载及使用默认配置。每种方法都提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.通过ProcessEngineConfiguration获取

package cn.lonecloud.mavenActivi;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;
/**
 * 通过使用ProcessEngineConfiguration获取
 * @Title: ConfigByClass.java
 * @Package cn.lonecloud.mavenActivi
 * @Description: 
 * @author lonecloud
 * @date 2016年8月22日 下午10:50:33
 */
public class ConfigByClass {
    @Test
    public void config() {
        //获取config对象
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration();
        //Jdbc设置
        String jdbcDriver = "com.mysql.jdbc.Driver";
        processEngineConfiguration.setJdbcDriver(jdbcDriver);
        String jdbcUrl = "jdbc:mysql://localhost:3306/mavenActiviti?useUnicode=true&characterEncoding=utf-8";
        processEngineConfiguration.setJdbcUrl(jdbcUrl);
        String jdbcUsername = "root";
        processEngineConfiguration.setJdbcUsername(jdbcUsername);
        String jdbcPassword = "123456";
        processEngineConfiguration.setJdbcPassword(jdbcPassword);
        /**
         * Checks the version of the DB schema against the library when the
         * process engine is being created and throws an exception if the
         * versions don't match.
         */
//        public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不自动创建新表

        /**
         * Creates the schema when the process engine is being created and drops
         * the schema when the process engine is being closed.
         */
//        public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//每次运行创建新表

        /**
         * Upon building of the process engine, a check is performed and an
         * update of the schema is performed if it is necessary.
         */
//        public static final String DB_SCHEMA_UPDATE_TRUE = "true";设置自动对表结构进行改进和升级
        //设置是否自动更新
        processEngineConfiguration
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        //获取引擎对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        processEngine.close();
    }
}

2.通过ProcessEngineConfiguration载入xml文件

xml文件:

<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">
    <!--这里的类太多别导错了 -->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 配置流程引擎配置对象 -->
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="jdbcUsername" value="root"></property>
        <property name="jdbcPassword" value="123456"></property>
        <!-- 注入数据源信息 -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>
    
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <!-- 注入自动建表设置 -->
        <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
    </bean>
</beans>

java代码

package cn.lonecloud.mavenActivi;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;
/**
 * 通过通过配置文件进行初始化获取引擎对
 * @Title: ConfigByConfig.java
 * @Package cn.lonecloud.mavenActivi
 * @Description: 
 * @author lonecloud
 * @date 2016年8月22日 下午10:40:35
 */
public class ConfigByConfig {
    /**
     * 通过配置文件进行初始化获取引擎对象
     * @Description:
     */
    @Test
    public void configByConf(){
        //载入资源
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //创建引擎
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        processEngine.getRepositoryService();
    }
 }

3.通过默认载入activiti.cfg.xml进行获取

* 通过默认载入activiti.cfg.xml进行获取
     * @Description:推荐使用
     */
    @Test
    public void configByDefault(){
        //通过获取载入默认获取
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.close();
    }

这里的xml文件名必须设置为activiti.cfg.xml

转载于:https://my.oschina.net/u/725790/blog/1539604

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值