浏览 6507 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-02-29
Spring, Springmodules, JBPM持久化集成理解系列一
【本系列如需转载,请注明作者及出处】
本系列文章假设阅者具备以下知识: 且具备以下材料: 1,Spring源代码 2,Hibernate源代码 3,Jbpm源代码 4,Springmodules源代码
<?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:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> <!-- framework's contexts --> <bean id="resource.PropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- JNDI DataSource for J2EE environments --> <!--jee:jndi-lookup id="dataSource" jndi-name="java:/comp/env/jdbc/moss" /--> <bean id="resource.DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${appserver.jdbc.driverClassName}" /> <property name="url" value="${appserver.jdbc.url}" /> <property name="username" value="${appserver.jdbc.username}" /> <property name="password" value="${appserver.jdbc.password}" /> <property name="maxActive" value="${appserver.jdbc.maxActive}" /> <property name="maxIdle" value="${appserver.jdbc.maxIdle}" /> <property name="maxWait" value="${appserver.jdbc.maxWait}" /> <property name="defaultAutoCommit" value="${appserver.jdbc.defaultAutoCommit}" /> <property name="removeAbandoned" value="${appserver.jdbc.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${appserver.jdbc.removeAbandonedTimeout}" /> </bean> <!-- Hibernate SessionFactory --> <bean id="resource.SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="resource.DataSource" /> <property name="configLocations"> <list> <value>classpath*:hibernate.cfg.xml</value> </list> </property> </bean> <!-- jBPM configuration --> <bean id="resource.JbpmConfiguration" class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean"> <property name="configuration" value="classpath:jbpm.cfg.xml" /> <property name="createSchema" value="false" /> <property name="sessionFactory"> <ref local="resource.SessionFactory"/> </property> </bean> <!-- jBPM template --> <bean id="resource.JbpmTemplate" class="org.springmodules.workflow.jbpm31.JbpmTemplate"> <constructor-arg index="0" ref="resource.JbpmConfiguration" /> </bean> </beans> LocalJbpmConfigurationFactoryBean 最重要的就是LocalJbpmConfigurationFactoryBean了,相信Spring的用户对这样的Bean都很熟悉。根据jbpmConfiguration配置文件和提供的sessionFactory,它会创建一个可以与提供的流程定义协同工作的jBPM Configuration。sessionFactory属性可有可无,如果提供了,将会使用sessionFactory提供的session进行数据库操作。但是如果注入了sessionFactory,就可以重用Spring提供的事务管理架构,且不需要对jBPM做任何代码修改,还可以结合使用OpenSessionInView模式,好处显而易见吧。 JbpmTemplate and JbpmCallback 像Spring与Hibernate,JDBC的集成有相应的Template, Callback类一样,Spring与Jbpm的集成也有JbpmTemplate和JbpmCallback,且默认提供了一些便利方法供用户使用,例如findProcessInstance,findPooledTaskInstances,saveProcessInstance,signal等。 因为JbpmTemplate继承了JbpmAccessor,父类JbpmAccessor实现了InitializingBean,所以Spring在初始化这个JbpmTemplate时会调用afterPropertiesSet方法来配置JbpmTemplate所使用到的HibernateTemplate。JbpmAccessor 还做了一些访问异常的转换操作。 这两个类协同起来要完成的事情是,如果提供了SessionFactory或者 HibernateTemplate, 在JbpmCallback里的doInJbpm方法之前注入SessionFactory提供的hibernate session。如果没有提供SessionFactory,那么持久化的操作还是交回给Jbpm。如果存在SessionFactory, session的打开,关闭就由SessionFactory管理。否则由JBPM自身的持久化逻辑来维护。 这部分逻辑可以查看HibernateTemplate的execute方法: public Object execute(final JbpmCallback callback) { final JbpmContext context = getContext(); try { // use the hibernateTemplate is present and if needed if (hibernateTemplate != null && hasPersistenceService) { // use hibernate template return hibernateTemplate.execute(new HibernateCallback() { /** * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session) */ public Object doInHibernate(Session session) throws HibernateException, SQLException { // inject the session in the context context.setSession(session); return callback.doInJbpm(context); } }); } // plain callback invocation (no template w/ persistence) return callback.doInJbpm(context); } catch (JbpmException ex) { throw convertJbpmException(ex); } finally { releaseContext(context); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-11-19
<bean id="resource.JbpmTemplate" 定义了但没有调用.
这是为什么? |
|
返回顶楼 | |
发表时间:2008-11-21
它是给你用的
|
|
返回顶楼 | |
发表时间:2008-12-29
如果不用JbpmTemplate,向下面这样调用对么?
private JbpmConfiguration jbpmConfiguration; // 获取JbpmContext对象 private JbpmContext getJbpmContext(){ JbpmContext context = jbpmConfiguration.createJbpmContext(); // 关键的一个步骤,我们需要将JBPM使用的session对象跟本系统所使用的 // ssion对象整合起来。 context.setSession(getSession()); return context; } public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) { this.jbpmConfiguration = jbpmConfiguration; } |
|
返回顶楼 | |