前面我們用代碼實現了生成25張activiti表,今天我們用Activiti提供的activiti.cfg.xml配置文件來簡化實現前面的功能;
官方文檔參考地址:http://activiti.org/userguide/index.html#configuration
我們先在src/test/resources下創建一個xml文件 名字是:activiti.cfg.xml
然后我們從官方文檔貼下參考的xml代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<
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
=
"processEngineConfiguration"
class
=
"org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"
>
<
property
name
=
"jdbcUrl"
value
=
"jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000"
/>
<
property
name
=
"jdbcDriver"
value
=
"org.h2.Driver"
/>
<
property
name
=
"jdbcUsername"
value
=
"sa"
/>
<
property
name
=
"jdbcPassword"
value
=
""
/>
<
property
name
=
"databaseSchemaUpdate"
value
=
"true"
/>
<
property
name
=
"jobExecutorActivate"
value
=
"false"
/>
<
property
name
=
"asyncExecutorEnabled"
value
=
"true"
/>
<
property
name
=
"asyncExecutorActivate"
value
=
"false"
/>
<
property
name
=
"mailServerHost"
value
=
"mail.my-corp.com"
/>
<
property
name
=
"mailServerPort"
value
=
"5025"
/>
</
bean
>
</
beans
>
|
這里的話,我們要根據我們的項目 修改jdbcUrl jdbcDriver jdbcUsername jdbcPassword 當然還有下面的配置我們可以去掉一些 后面會降到這些配置的用途;
修改完后的xml如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
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
=
"processEngineConfiguration"
class
=
"org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"
>
<
property
name
=
"jdbcUrl"
value
=
"jdbc:mysql://localhost:3306/db_activiti"
/>
<
property
name
=
"jdbcDriver"
value
=
"com.mysql.jdbc.Driver"
/>
<
property
name
=
"jdbcUsername"
value
=
"root"
/>
<
property
name
=
"jdbcPassword"
value
=
"123456"
/>
<
property
name
=
"databaseSchemaUpdate"
value
=
"true"
/>
</
bean
>
</
beans
>
|
接下來我們就是要通過代碼來讀取配置文件,然后獲取工作流引擎實例:
代碼如下:
1
2
3
4
5
6
7
8
9
10
|
/**
* 使用xml配置 簡化
*/
@Test
public
void
testCreateTableWithXml(){
// 引擎配置
ProcessEngineConfiguration pec=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(
"activiti.cfg.xml"
);
// 獲取流程引擎對象
ProcessEngine processEngine=pec.buildProcessEngine();
}
|
然后我們測試的時候 先把前面db_activiti數據下的表 全部刪除;
然后運行上面的測試類,我們會發現 表自動生成了:
表依然是前面的25張表;
我們會發現,使用xml配置會簡化很多東西。。
完整代碼打包下載:http://pan.baidu.com/s/1nuSJ3kd