jbpm4.0整合spring+hibernate
??? 首先加入jbpm4.0相关jar包。
?
??? 修改jbpm.spring.default.cfg.xml?注释掉以下部分(让jbpm 的session-factory失效)
<hibernate-configuration> <cfg resource="jbpm.hibernate.cfg.xml" /></hibernate-configuration><hibernate-session-factory />
??? ?
????建立一个ProcessEngineFactoryBean类(用来初始化JBPM4的相关配置文件、工作流引擎,并且把sessionFactory的创建交给spring的容器)
???
package com.rich.oa.jbpm;import org.hibernate.SessionFactory;import org.jbpm.api.ProcessEngine;import org.jbpm.pvm.internal.cfg.SpringConfiguration;import org.springframework.beans.BeansException;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.FactoryBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class ProcessEngineFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware { private String jbpmConfigurationLocation = "jbpm.cfg.xml"; private SessionFactory sessionFactory; private ApplicationContext applicationContext; private ProcessEngine processEngine; public Object getObject() throws Exception { return processEngine; } @SuppressWarnings("unchecked") public Class getObjectType() { return ProcessEngine.class; } public boolean isSingleton() { return true; } public void afterPropertiesSet() { SpringConfiguration cfg = new SpringConfiguration(jbpmConfigurationLocation); cfg.setApplicationContext(applicationContext); cfg.setSessionFactory(sessionFactory); this.processEngine = cfg.buildProcessEngine(); } public void destroy() { this.processEngine = null; this.sessionFactory = null; } public void setJbpmConfigurationLocation( String jbpmConfigurationLocation) { this.jbpmConfigurationLocation = jbpmConfigurationLocation; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void setApplicationContext( ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }}??
?? 并且在Spring配置文件中加入如下配置:
??
<bean id="processEngine" ref="sessionFactory" /></bean>
?
????
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" /> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
?
???添加JBPM4的服务到spring IOC容器中错误:
?? 注意:所有的jbpm4的配置文件必须在src目录下,并且不能有独立的目录,因为在初始化发布流程的代码中
ProcessEngine processEngine = new Configuration().buildProcessEngine();
因为JbpmConfiguration extends Configuration implements Context, ProcessEngine, EnvironmentFactory? 因此会
调用JbpmConfiguration类中buildProcessEngine()方法来创建ProcessEngine来得到JBPM4的服务,在这个方法中,有这样一段代码
???
if (!isConfigured) { setResource("jbpm.cfg.xml"); }?
????
?? 它会去src下找jbpm.cfg.xml文件,如果配置文件不在src目录下,那么就会找不到配置文件(jbpm.cfg.xml)
?
?? 加入JBPM4的实体关系映射文件:
??
<property name="mappingLocations"> <list> <value>classpath*:jbpm.repository.hbm.xml</value> <value>classpath*:jbpm.execution.hbm.xml</value> <value>classpath*:jbpm.history.hbm.xml</value> <value>classpath*:jbpm.task.hbm.xml</value> <value>classpath*:jbpm.jpdl.hbm.xml</value> <value>classpath*:jbpm.identity.hbm.xml</value> </list></property>
?
??改写Jbpm4中的jbpm.tx.hibernate.cfg.xml的配置,主要是修改processEngine 这个bean里的sessionFactory.从而再加上上面这种方式来进行事务的管理。
???
<jbpm-configuration><process-engine-context><command-service><retry-interceptor /><environment-interceptor /><spring-transaction-interceptor /></command-service></process-engine-context><transaction-context><hibernate-session current="true" /></transaction-context></jbpm-configuration>
?? 修改 <standard-transaction-interceptor />为<spring-transaction-interceptor />
<hibernate-session /><transaction />为<hibernate-session current="true"/>
current="true"这个属性将强使jbpm搜索当前的spring提供的session。
??
?? hibernateSessionFactory上配置上Jpbm的hbm.xml文件。如:
??
<property name="mappingLocations"> <list><value>classpath:jbpm.repository.hbm.xml</value> <value>classpath:jbpm.execution.hbm.xml</value> <value>classpath:jbpm.history.hbm.xml</value> <value>classpath:jbpm.task.hbm.xml</value> <value>classpath:jbpm.jpdl.hbm.xml</value> <value>classpath:jbpm.identity.hbm.xml</value></list></property>
???
?? 还需要在jbpm.default.cfg.xml中注释以下代码:
??
<!-- <hibernate-configuration> <cfg resource="jbpm.hibernate.cfg.xml" /> </hibernate-configuration> <hibernate-session-factory />-->
????
??? ok,测试通过!
?