?????? 現在流行抱大腿,不過對眼光的要求也高。要不就如高也,即使四眼,一樣無用。對Java企業開發而言,Spring的腿則是一定要抱的。而所謂抱Spring的腿,無外乎三點:
一是通過Spring暴露出服務,將服務配置到Spring的IOC容器里;
二是在自己的運行環境里訪問到Spring的IOC容器,能夠輕松使用Spring容器里所配置的服務;
三是對于具有事務管理特性的項目來說,將事務管理與Spring的事務管理進行合并。
??????? 下面分別討論:
一、??? 通過Spring暴露服務
還記得在jBPM4的運行期環境里提到的JbpmConfiguration嗎?它是整個jBPM4的入口,并且是整個應用獨此一份的。通過它可以獲取processEngine,并藉此獲得工作流引擎所提供的各種服務:
ProcessEngine?processEngine?=?new?Configuration()
??????.buildProcessEngine();
RepositoryService?repositoryService?=?processEngine.getRepositoryService();
ExecutionService?executionService?=?processEngine.getExecutionService();
TaskService?taskService?=?processEngine.getTaskService();
HistoryService?historyService?=?processEngine.getHistoryService();
ManagementService?managementService?=?processEngine.getManagementService();
通過Spring暴露這些服務,配置如下:
細心的你會發現,配置時使用了JbpmConfiguration 的子類SpringConfiguration。SpringConfiguration相比JbpmConfiguration有哪些增強呢,下面再 講??傊F在,就可以使用Spring來獲取或注入這些Jbpm4所提供的服務了。
二、在environment里加入SpringContext
jBPM4的environment(運行期環境)提供Engine IOC(process-engine-context)和Transaction IOC(transaction-context)。要想在運行期方便地訪問到Spring里所配置的服務,最直接的方法就是在environment里 加入Spring IOC(applicationContext)的引用。
SpringConfiguration即是對JbpmConfiguration增強了對Spring IOC的一個引用。
?
SpringConfiguration是如何做到的呢?簡單,實現Spring的ApplicationContextAware接口,自動持有applicationContext,然后openEnvironment時將其加入environment。
SpringContext是對applicationContext的簡單封裝。
那么什么從Engine IOC移民到Spring IOC了呢?是的,最重要的就是Hibernate Session Factory。
在jbpm.cfg.xml的process-engine-context里干掉:
?
相關配置挪動至Spring配置文件。
三、??? 事務
哪里有數據庫操作,哪里就有事務。對于嵌入式工作流而言,最重要的集成就是事務的集成。這里先分析jBPM4的事務實現,然后再介紹集成入Spring的事務實現。
1、??? Command模式
jBPM4的邏輯實現采用了Command模式。
?
采用Command模式后,jBPM4對CommandService構造攔截器(Interceptor)鏈,配置在jbpm.cfg.xml的process-engine-context里:
2、??? 原有的事務實現
jBPM4原有的事務通過StandardTransactionInterceptor實現,在CommandService執行Command之前打開事務(實際委派Hibernate的事務管理),完成后提交/回滾。
?
jBPM4的事務是基于Command的。
3、??? 集成入Spring的事務實現
Spring的事務是基于服務調用的。

使jBPM4使用Spring提供的事務:
攔截器換用SpringTransactionInterceptor,SpringTransactionInterceptor從environment 提供的Spring IOC獲取PlatformTransactionManager,使用事務模板回調Command,事務傳播模式強制加入當前事務。
同時,對hibernate session的配置(jbpm.cfg.xml的transaction-context)強制從當前線程中獲?。?br />
并干掉原有的事務實現:
參考文檔:
http://www.slideshare.net/guest8d4bce/spring-integration-with-jbpm4
一是通過Spring暴露出服務,將服務配置到Spring的IOC容器里;
二是在自己的運行環境里訪問到Spring的IOC容器,能夠輕松使用Spring容器里所配置的服務;
三是對于具有事務管理特性的項目來說,將事務管理與Spring的事務管理進行合并。
??????? 下面分別討論:
一、??? 通過Spring暴露服務
還記得在jBPM4的運行期環境里提到的JbpmConfiguration嗎?它是整個jBPM4的入口,并且是整個應用獨此一份的。通過它可以獲取processEngine,并藉此獲得工作流引擎所提供的各種服務:
ProcessEngine?processEngine?=?new?Configuration()
??????.buildProcessEngine();
RepositoryService?repositoryService?=?processEngine.getRepositoryService();
ExecutionService?executionService?=?processEngine.getExecutionService();
TaskService?taskService?=?processEngine.getTaskService();
HistoryService?historyService?=?processEngine.getHistoryService();
ManagementService?managementService?=?processEngine.getManagementService();
通過Spring暴露這些服務,配置如下:
<bean?id="jbpmConfiguration"?class="org.jbpm.pvm.internal.cfg.SpringConfiguration">
????????<constructor-arg?value="be/inze/spring/demo/jbpm.cfg.xml"?/>
????</bean>
???
????<bean?id="processEngine"?factory-bean="jbpmConfiguration"?factory-method="buildProcessEngine"?/>
????<bean?id="repositoryService"?factory-bean="processEngine"?factory-method="getRepositoryService"?/>
????<bean?id="executionService"?factory-bean="processEngine"?factory-method="getExecutionService"?/>
????????<constructor-arg?value="be/inze/spring/demo/jbpm.cfg.xml"?/>
????</bean>
???
????<bean?id="processEngine"?factory-bean="jbpmConfiguration"?factory-method="buildProcessEngine"?/>
????<bean?id="repositoryService"?factory-bean="processEngine"?factory-method="getRepositoryService"?/>
????<bean?id="executionService"?factory-bean="processEngine"?factory-method="getExecutionService"?/>
細心的你會發現,配置時使用了JbpmConfiguration 的子類SpringConfiguration。SpringConfiguration相比JbpmConfiguration有哪些增強呢,下面再 講??傊F在,就可以使用Spring來獲取或注入這些Jbpm4所提供的服務了。
二、在environment里加入SpringContext
jBPM4的environment(運行期環境)提供Engine IOC(process-engine-context)和Transaction IOC(transaction-context)。要想在運行期方便地訪問到Spring里所配置的服務,最直接的方法就是在environment里 加入Spring IOC(applicationContext)的引用。
SpringConfiguration即是對JbpmConfiguration增強了對Spring IOC的一個引用。
?

SpringConfiguration是如何做到的呢?簡單,實現Spring的ApplicationContextAware接口,自動持有applicationContext,然后openEnvironment時將其加入environment。
environment.setContext(new?SpringContext(applicationContext));
SpringContext是對applicationContext的簡單封裝。
那么什么從Engine IOC移民到Spring IOC了呢?是的,最重要的就是Hibernate Session Factory。
在jbpm.cfg.xml的process-engine-context里干掉:
????<hibernate-configuration>
??????<cfg?resource="jbpm.hibernate.cfg.xml"?/>????
????</hibernate-configuration>
????<hibernate-session-factory?/>
??????<cfg?resource="jbpm.hibernate.cfg.xml"?/>????
????</hibernate-configuration>
????<hibernate-session-factory?/>
?
相關配置挪動至Spring配置文件。
三、??? 事務
哪里有數據庫操作,哪里就有事務。對于嵌入式工作流而言,最重要的集成就是事務的集成。這里先分析jBPM4的事務實現,然后再介紹集成入Spring的事務實現。
1、??? Command模式
jBPM4的邏輯實現采用了Command模式。
?

采用Command模式后,jBPM4對CommandService構造攔截器(Interceptor)鏈,配置在jbpm.cfg.xml的process-engine-context里:
<command-service>
??????<retry-interceptor?/>
??????<environment-interceptor?/>
??????<standard-transaction-interceptor?/>
????</command-service>
??????<retry-interceptor?/>
??????<environment-interceptor?/>
??????<standard-transaction-interceptor?/>
????</command-service>
2、??? 原有的事務實現
jBPM4原有的事務通過StandardTransactionInterceptor實現,在CommandService執行Command之前打開事務(實際委派Hibernate的事務管理),完成后提交/回滾。
?

jBPM4的事務是基于Command的。
3、??? 集成入Spring的事務實現
Spring的事務是基于服務調用的。

使jBPM4使用Spring提供的事務:
<command-service>
??????<retry-interceptor?/>
??????<environment-interceptor?/>
??????<spring-transaction-interceptor?current="true"?/>
</command-service>
??????<retry-interceptor?/>
??????<environment-interceptor?/>
??????<spring-transaction-interceptor?current="true"?/>
</command-service>
攔截器換用SpringTransactionInterceptor,SpringTransactionInterceptor從environment 提供的Spring IOC獲取PlatformTransactionManager,使用事務模板回調Command,事務傳播模式強制加入當前事務。
同時,對hibernate session的配置(jbpm.cfg.xml的transaction-context)強制從當前線程中獲?。?br />
<hibernate-session?current="true"/>
并干掉原有的事務實現:
<transaction?/>
參考文檔:
http://www.slideshare.net/guest8d4bce/spring-integration-with-jbpm4