Spring 集成 jBPM4.3 主要工作有2点:
1. 将jbpm默认的hibernate事务换成spring事务,即在jbpm.cfg.xml文件中将
<import resource="jbpm.tx.hibernate.cfg.xml" />
替换成:
<import resource="jbpm.tx.spring.cfg.xml" />
2. jBPM的核心控件processEngine由Spring生成、管理。
这2方面的内容,网上有很多资料查询,但基本都是在xml文件中进行配置,而我是在之前的代码中集成jBPM,之前的代码采用的是注解的方式(Spring注解,Hibernate注解),所以采用编码的方式获得了processEngine对象,即:
@Bean(name="processEngine")
@DependsOn("sessionFactory")
public ProcessEngine getProcessEngine() throws Exception{
processEngine = new ConfigurationImpl().springEnabled()
.setHibernateSessionFactory(this.getSessionFactory())
.springInitiated(applicationContext)
.buildProcessEngine();
return processEngine;
}
从上述代码中可以看到,生成processEngine对象需要设置SessionFactory对象。查看jBPM4.3源码SpringProcessEngine.java类,发现其获取的是LocalSessionFactoryBena对象,而我在代码中采用的是注解方式,所以生成的是AnnotationSessionFactoryBean对象,即:
@Bean(name="annotationSessionFactoryBean")
@DependsOn("dataSource")
public AnnotationSessionFactoryBean getAnnotationSessionFactoryBean() throws Exception {
Properties properties = new Properties();
properties.put("hibernate.dialect", dialect);
properties.put("hibernate.show_sql", showSql);
properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
properties.put("hibernate.max_fetch_depth", max_fetch_depth);
properties.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
properties.put("hibernate.jdbc.fetch_size", "50");
properties.put("hibernate.jdbc.batch_size", "25");
AnnotationSessionFactoryBean a = new AnnotationSessionFactoryBean();
a.setDataSource(this.getDataSource());
a.setHibernateProperties(properties);
a.setPackagesToScan(models.split(","));
// jbpm文件
a.setMappingResources(new String[]{"jbpm.repository.hbm.xml","jbpm.execution.hbm.xml","jbpm.history.hbm.xml","jbpm.task.hbm.xml","jbpm.identity.hbm.xml"});
a.afterPropertiesSet();
return a;
}
解决的办法是在本地源码中新建一个对应的SpringProcessEngine类,用以覆盖jBPM源码。修改其中的代码,获取AnnotationSessionFactoryBean对象。以下是修改的create方法:
public static ProcessEngine create(ConfigurationImpl configuration) {
SpringProcessEngine springProcessEngine = null;
ApplicationContext applicationContext = null;
if (configuration.isInstantiatedFromSpring()) {
applicationContext = (ApplicationContext) configuration.getApplicationContext();
springProcessEngine = new SpringProcessEngine();
springProcessEngine.applicationContext = applicationContext;
springProcessEngine.initializeProcessEngine(configuration);
// 原先的代码
// LocalSessionFactoryBean localSessionFactoryBean = springProcessEngine.get(LocalSessionFactoryBean.class);
// Configuration hibernateConfiguration = localSessionFactoryBean.getConfiguration();
// springProcessEngine.processEngineWireContext
// .getWireDefinition()
// .addDescriptor(new ProvidedObjectDescriptor(hibernateConfiguration, true));
//
// springProcessEngine.checkDb(configuration);
// 将LocalSessionFactoryBean 换成 AnnotationSessionFactoryBean
AnnotationSessionFactoryBean annotationSessionFactoryBean = springProcessEngine.get(AnnotationSessionFactoryBean.class);
Configuration hibernateConfiguration = annotationSessionFactoryBean.getConfiguration();
springProcessEngine.processEngineWireContext
.getWireDefinition()
.addDescriptor(new ProvidedObjectDescriptor(hibernateConfiguration, true));
springProcessEngine.checkDb(configuration);
} else {
String springCfg = (String) configuration.getProcessEngineWireContext().get("spring.cfg");
if (springCfg==null) {
springCfg = "applicationContext.xml";
}
applicationContext = new ClassPathXmlApplicationContext(springCfg);
springProcessEngine = (SpringProcessEngine) applicationContext.getBean("processEngine");
}
return springProcessEngine;
}
本地新建的类: