1.通过ProcessEngineConfiguration获取
1 package cn.lonecloud.mavenActivi; 2 3 import org.activiti.engine.ProcessEngine; 4 import org.activiti.engine.ProcessEngineConfiguration; 5 import org.junit.Test; 6 /** 7 * 通过使用ProcessEngineConfiguration获取 8 * @Title: ConfigByClass.java 9 * @Package cn.lonecloud.mavenActivi 10 * @Description: 11 * @author lonecloud 12 * @date 2016年8月22日 下午10:50:33 13 */ 14 public class ConfigByClass { 15 @Test 16 public void config() { 17 //获取config对象 18 ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration 19 .createStandaloneProcessEngineConfiguration(); 20 //Jdbc设置 21 String jdbcDriver = "com.mysql.jdbc.Driver"; 22 processEngineConfiguration.setJdbcDriver(jdbcDriver); 23 String jdbcUrl = "jdbc:mysql://localhost:3306/mavenActiviti?useUnicode=true&characterEncoding=utf-8"; 24 processEngineConfiguration.setJdbcUrl(jdbcUrl); 25 String jdbcUsername = "root"; 26 processEngineConfiguration.setJdbcUsername(jdbcUsername); 27 String jdbcPassword = "123456"; 28 processEngineConfiguration.setJdbcPassword(jdbcPassword); 29 /** 30 * Checks the version of the DB schema against the library when the 31 * process engine is being created and throws an exception if the 32 * versions don‘t match. 33 */ 34 // public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不自动创建新表 35 36 /** 37 * Creates the schema when the process engine is being created and drops 38 * the schema when the process engine is being closed. 39 */ 40 // public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//每次运行创建新表 41 42 /** 43 * Upon building of the process engine, a check is performed and an 44 * update of the schema is performed if it is necessary. 45 */ 46 // public static final String DB_SCHEMA_UPDATE_TRUE = "true";设置自动对表结构进行改进和升级 47 //设置是否自动更新 48 processEngineConfiguration 49 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 50 //获取引擎对象 51 ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); 52 processEngine.close(); 53 } 54 }
2.通过ProcessEngineConfiguration载入xml文件
xml文件:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 5 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 6 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 7 <!--这里的类太多别导错了 --> 8 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 9 <!-- 配置流程引擎配置对象 --> 10 <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> 11 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8"></property> 12 <property name="jdbcUsername" value="root"></property> 13 <property name="jdbcPassword" value="123456"></property> 14 <!-- 注入数据源信息 --> 15 <property name="databaseSchemaUpdate" value="true"></property> 16 </bean> 17 18 <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> 19 <!-- 注入自动建表设置 --> 20 <property name="processEngineConfiguration" ref="processEngineConfiguration"></property> 21 </bean> 22 </beans>
java
1 package cn.lonecloud.mavenActivi; 2 3 import org.activiti.engine.ProcessEngine; 4 import org.activiti.engine.ProcessEngineConfiguration; 5 import org.activiti.engine.ProcessEngines; 6 import org.junit.Test; 7 /** 8 * 通过通过配置文件进行初始化获取引擎对 9 * @Title: ConfigByConfig.java 10 * @Package cn.lonecloud.mavenActivi 11 * @Description: 12 * @author lonecloud 13 * @date 2016年8月22日 下午10:40:35 14 */ 15 public class ConfigByConfig { 16 /** 17 * 通过配置文件进行初始化获取引擎对象 18 * @Description: 19 */ 20 @Test 21 public void configByConf(){ 22 //载入资源 23 ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml"); 24 //创建引擎 25 ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); 26 processEngine.getRepositoryService(); 27 } 28 }
3.通过默认载入activiti.cfg.xml进行获取
1 * 通过默认载入activiti.cfg.xml进行获取 2 * @Description:推荐使用 3 */ 4 @Test 5 public void configByDefault(){ 6 //通过获取载入默认获取 7 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 8 processEngine.close(); 9 }
这里的xml文件名必须设置为activiti.cfg.xml
时间: 2024-10-10 17:46:55