Activiti獲取ProcessEngine的三種方法
阿新 • • 發佈:2018-11-14
1.通過ProcessEngineConfiguration獲取
package cn.lonecloud.mavenActivi; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.junit.Test; /** * 通過使用ProcessEngineConfiguration獲取 */ 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&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; /** * 通過通過配置檔案進行初始化獲取引擎對 */ 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進行獲取
@Test
public void configByDefault(){
//通過獲取載入預設獲取
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
processEngine.close();
}
這裡的xml檔名必須設定為activiti.cfg.xml