Spring2.5整合Activiti 5.11 解决MalformedParameterizedTypeException问题
关于Spring3.X与Activiti5.11整合配置如下:
<!-- Activiti begin --><bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"><!-- <property name="jdbcUrl" value="${jdbc.url}" /> --><!-- <property name="jdbcDriver" value="${jdbc.driver}" /> --><!-- <property name="jdbcUsername" value="${jdbc.username}" /> --><!-- <property name="jdbcPassword" value="${jdbc.password}" /> --> <property name="dataSource" ref="dataSource" /><property name="transactionManager" ref="transactionManager" /><property name="databaseSchemaUpdate" value="true" /><!-- 测试情况下关闭多线程 --><property name="jobExecutorActivate" value="false" /><property name="history" value="full" /><!-- <property name="mailServerHost" value="mail.my-corp.com" /> --><!-- <property name="mailServerPort" value="5025" /> --><!-- 自动部署 --><property name="deploymentResources"><list><value>classpath*:/diagrams/*</value></list></property></bean><bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"><property name="processEngineConfiguration" ref="processEngineConfiguration" /></bean><bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /><bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /><bean id="formService" factory-bean="processEngine" factory-method="getFormService" /><bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /><bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /><bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /><bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /><!-- Activiti end -->
这种配置采用activiti-spring插件实现,在Spring 3.X中没有问题。
但在Spring2.5使用如下配置,则会引发 MalformedParameterizedTypeException。
由于Spring2.5的FactoryBean(即上述的ProcessEngineFactoryBean)不支持泛型,Spring尝试获取该泛型类型时会产生该异常。
解决方案:
<!-- Activiti begin --><bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /><property name="transactionManager" ref="transactionManager" /><property name="databaseSchemaUpdate" value="false" /><property name="jobExecutorActivate" value="false" /><property name="history" value="full" /><property name="deploymentResources"><list><value>classpath*:/diagrams/*</value></list></property><property name="transactionsExternallyManaged" value="true" /><!-- Spring表达式支持 --><property name="expressionManager"><bean class="com.hxchn.aero.util.SpringExpressionManagerFactoryBean"><property name="processEngineConfiguration" ref="processEngineConfiguration"></property></bean></property></bean><bean id="processEngine" factory-bean="processEngineConfiguration" factory-method="buildProcessEngine"></bean><bean id="repositoryService" factory-bean="processEngineConfiguration" factory-method="getRepositoryService" /><bean id="runtimeService" factory-bean="processEngineConfiguration" factory-method="getRuntimeService" /><bean id="formService" factory-bean="processEngineConfiguration" factory-method="getFormService" /><bean id="identityService" factory-bean="processEngineConfiguration" factory-method="getIdentityService" /><bean id="taskService" factory-bean="processEngineConfiguration" factory-method="getTaskService" /><bean id="historyService" factory-bean="processEngineConfiguration" factory-method="getHistoryService" /><bean id="managementService" factory-bean="processEngineConfiguration" factory-method="getManagementService" /><!-- Activiti end -->
com.hxchn.aero.util.SpringExpressionManagerFactoryBeancom.hxchn.aero.util.SpringExpressionManagerFactoryBean代码如下:
package com.hxchn.aero.util;import org.activiti.engine.ProcessEngineConfiguration;import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;import org.activiti.spring.SpringExpressionManager;import org.springframework.beans.BeansException;import org.springframework.beans.factory.FactoryBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class SpringExpressionManagerFactoryBean implements FactoryBean, ApplicationContextAware {ApplicationContext applicationContext;ProcessEngineConfiguration processEngineConfiguration;@Overridepublic Object getObject() throws Exception {return new SpringExpressionManager(applicationContext, ((ProcessEngineConfigurationImpl) processEngineConfiguration).getBeans());}@Overridepublic Class getObjectType() {return SpringExpressionManager.class;}@Overridepublic boolean isSingleton() {return true;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {this.applicationContext = applicationContext;}public void setProcessEngineConfiguration(ProcessEngineConfiguration processEngineConfiguration) {this.processEngineConfiguration = processEngineConfiguration;}}
以上避免使用ProcessEngineFactoryBean, 而通过使用processEngineConfiguration的工厂方法buildProcessEngine的方式获取processEngine流程引擎。
对于早期基于Spring2.5的项目引入Activiti可以采用该方式;我现在项目也是基于Spring2.5.6开发的,也是通过该配置方式引入Activiti5.11。
对于新项目建议使用Spring3.X以上,可以直接使用activti-spring插件,网上也有一些例子,配置学习起来更为方便。