---
title: "JBPM 与 Spring 结合"
author: "Jamsa"
date: "2008-05-14T16:56:13Z"
tags: []
categories: ["企业架构"]
slug: "jbpm-spring"
draft: false
lastmod: "2008-05-14T16:56:13Z"
id: 192996
url: "https://jamsa.iteye.com/blog/192996"
---

今天尝试了将jbpm和spring进行结合，主要参考http://betafox.iteye.com/blog/177649来进行。

版本：

jbpm          3.1.4

struts2       2.0.11

spring        2.5.1

hibernate   3.2.5.ga

配置文件如下：

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"
	default-autowire="byName">

	<bean id="workflow1"
		class="org.springmodules.workflow.jbpm31.definition.ProcessDefinitionFactoryBean">
		<property name="definitionLocation"
			value="classpath:jbpm/processes/processdefinition.xml" />
	</bean>

	<!-- jBPM configuration-->
	<bean id="jbpmConfiguration"
		class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="configuration" value="classpath:jbpm.cfg.xml" />

		<property name="processDefinitions">
			<list>
				<ref local="workflow1" />
			</list>
		</property>
		<!-- 
			<property name="createSchema" value="true" />
			<property name="dropSchema" value="true" />
		-->
	</bean>

	<!-- jBPM template -->
	<bean id="jbpmTemplate"
		class="org.springmodules.workflow.jbpm31.JbpmTemplate">
		<constructor-arg index="0" ref="jbpmConfiguration" />
		<constructor-arg index="1" ref="workflow1" />
	</bean>

</beans>
```

 在casspath下添加了jbpm.cfg.xml，内容是从default.jbpm.cfg.xml中复制过来的：

```
<jbpm-configuration>

	<jbpm-context name="default.jbpm.context">

		<service name="persistence"
			factory="org.jbpm.persistence.db.DbPersistenceServiceFactory" />
		<service name="message"
			factory="org.jbpm.msg.db.DbMessageServiceFactory" />
		<service name="scheduler"
			factory="org.jbpm.scheduler.db.DbSchedulerServiceFactory" />
		<service name="logging"
			factory="org.jbpm.logging.db.DbLoggingServiceFactory" />
		<service name="authentication"
			factory="org.jbpm.security.authentication.DefaultAuthenticationServiceFactory" />
	</jbpm-context>


	<!-- 不再使用<string name="resource.hibernate.cfg.xml" value="hibernate.cfg.xml" />-->
	<string name="resource.business.calendar"
		value="org/jbpm/calendar/jbpm.business.calendar.properties" />
	<string name="resource.default.modules"
		value="org/jbpm/graph/def/jbpm.default.modules.properties" />
	<string name="resource.converter"
		value="org/jbpm/db/hibernate/jbpm.converter.properties" />
	<string name="resource.action.types"
		value="org/jbpm/graph/action/action.types.xml" />
	<string name="resource.node.types"
		value="org/jbpm/graph/node/node.types.xml" />
	<string name="resource.parsers"
		value="org/jbpm/jpdl/par/jbpm.parsers.xml" />
	<string name="resource.varmapping"
		value="org/jbpm/context/exe/jbpm.varmapping.xml" />

</jbpm-configuration>
```

 

在配置过程中也遇到了一些问题。

问题一：系统报找不到 default.jbpm.context。

解决方法：在自己的jbpm.cfg.xml中设置

```
jbpm-context name="default.jbpm.context"
```

问题二：系统报找不到hibernate.cfg.xml。

解决方法：我的系统里hibernate配置放在spring的配置文件里，没有使用hibernate.cfg.xml。跟踪DbPersistenceServiceFactory和LocalJbpmConfigurationFactoryBean.afterPropertiesSet发现问题出在jbpmContext构建时需要获取持久层，如果sessionFactory存在则不会去读取hibernate.cfg.xml，除了在配置jbpmConfiguration时要设置sessionFactory外，如果jbpm的数据库表已经存在，则不要设置createSchema和dropSchema否则会报错。按上面的配置文件设置后，不会再去读取hibernate.cfg.xml，可以注释掉jbpm.cfg.xml中的resource.hibernate.cfg.xml。

注意：如果使用这种配置方式，省略了hibernate.cfg.xml文件，但如果调用jbpmConfiguration.createSchema();jbpmConfiguration.dropSchema();等方法还是会出错，跟踪源码可以发现这些方法都会调用DbPersistenceServiceFactory.getConfiguration()方法，这个方法会去读取hibernate.cfg.xml。我们可以使用jbpm-db\\build\\mysql\\scripts\\下的脚本手工创建或删除数据库，这在做单元测试时稍显麻烦。

问题三：jbpm-identity中有User.hbm.xml，这个名称与我系统中原有的User.hbml.xml冲突，尽管他们不在同一个包中，但还是产生了冲突。只能调整一下自己的代码了。

spring配置文件中配置流程定义的xml有些多余，反而使系统在启动的时候更新数据库里的流程定义。完全可以将流程部署到数据库。JbpmTemplate有无参数的构造器，可以不使用构造器注入，只需要在spring配置文件中注它的jbpmConfiguration属性就可以了。

 

另，没有添加参考文章里说的过滤器，暂时没发现问题。