工作流Flowable和Spring集成时,有一个xml是用来配置ProcessEngine的信息,xml的内容如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- Activiti begin --> <bean id="processEngineConfiguration" class="com.xxx.xxx.config.ExtProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="databaseSchemaUpdate" value="true" /> <!-- property name="jobExecutorActivate" value="false" /--> <property name="customSessionFactories"> <list> <bean class="com.xxx.xxx.entity.EntityManagerFactory"> <constructor-arg index="0" value="org.flowable.idm.engine.impl.persistence.entity.UserEntityManager" /> <constructor-arg index="1"> <bean class="com.xxx.xxx.entity.SmartUserEntityManager"/> </constructor-arg> </bean> <bean class="com.xxx.xxx.entity.EntityManagerFactory"> <constructor-arg index="0" value="org.flowable.idm.engine.impl.persistence.entity.GroupEntityManager" /> <constructor-arg index="1"> <bean class="com.xxx.xxx.entity.SmartGroupEntityManager"/> </constructor-arg> </bean> <bean class="com.xxx.xxx.entity.EntityManagerFactory"> <constructor-arg index="0" value="org.flowable.idm.engine.impl.persistence.entity.MembershipEntityManager" /> <constructor-arg index="1"> <bean class="com.xxx.xxx.entity.SmartMembershipEntityManager"/> </constructor-arg> </bean> </list> </property> </bean> <bean id="processEngine" factory-bean="processEngineConfiguration" factory-method="buildProcessEngine" /> <bean id="repositoryService" factory-bean="processEngineConfiguration" factory-method="getRepositoryService" /> <bean id="runtimeService" factory-bean="processEngineConfiguration" factory-method="getRuntimeService" /> <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" /> <bean id="formService" factory-bean="processEngineConfiguration" factory-method="getFormService" /> <bean id="identityService" factory-bean="processEngineConfiguration" factory-method="getIdentityService" /> <bean id="commandExecutor" factory-bean="processEngineConfiguration" factory-method="getCommandExecutor" /> <bean id="processDefinitionCache" factory-bean="processEngineConfiguration" factory-method="getProcessDefinitionCache"/> </beans>使用Spring boot之后推崇java配置取代xml配置,要对这个xml进行改造首先得搞清楚这个xml里面的内容,主要是定义了ExtProcessEngineConfiguration和它相关的属性,
说明: constructor-arg:通过构造函数注入。 property:通过setter对应的方法注入。 factory-method:通过工厂方法来构造java Bean
创建config配置文件WorkflowConfiguration,在这个类中通过@bean的注解构造ExtProcessEngineConfiguration 类,同时设置ExtProcessEngineConfiguration 需要的几个属性
@Configuration public class WorkflowConfiguration { private final DataSource dataSource; private final PlatformTransactionManager transactionManager; public WorkflowConfiguration(DataSource dataSource,PlatformTransactionManager transactionManager){ this.dataSource = dataSource; this.transactionManager = transactionManager; } @Bean public SmartUserEntityManager smartUserEntityManager() { return new SmartUserEntityManager(new ExtProcessEngineConfiguration()); } @Bean public SmartGroupEntityManager smartGroupEntityManager() { return new SmartGroupEntityManager(new ExtProcessEngineConfiguration()); } @Bean public SmartMembershipEntityManager smartMembershipEntityManager() { return new SmartMembershipEntityManager(new ExtProcessEngineConfiguration()); } public EntityManagerFactory userEntityManagerFactory(){ EntityManagerFactory entityManagerFactory = new EntityManagerFactory(UserEntityManager.class, smartUserEntityManager()); return entityManagerFactory; } public EntityManagerFactory groupEntityManagerFactory(){ EntityManagerFactory entityManagerFactory = new EntityManagerFactory(GroupEntityManager.class, smartGroupEntityManager()); return entityManagerFactory; } public EntityManagerFactory membershipEntityManagerFactory(){ EntityManagerFactory entityManagerFactory = new EntityManagerFactory(MembershipEntityManager.class, smartMembershipEntityManager()); return entityManagerFactory; } @Bean public ExtProcessEngineConfiguration engineConfiguration(){ ExtProcessEngineConfiguration engineConfiguration = new ExtProcessEngineConfiguration(); engineConfiguration.setDatabaseSchemaUpdate("true"); engineConfiguration.setDataSource(dataSource); engineConfiguration.setTransactionManager(transactionManager); List<SessionFactory> customSessionFactories = new ArrayList<>(); customSessionFactories.add(userEntityManagerFactory()); customSessionFactories.add(groupEntityManagerFactory()); customSessionFactories.add(membershipEntityManagerFactory()); engineConfiguration.setCustomSessionFactories(customSessionFactories); return engineConfiguration; } }其中核心的代码是:
@Bean public ExtProcessEngineConfiguration engineConfiguration()这个方法。
其中有一个重点需要注意的地方是,在xml里面有一段:
<bean id="processEngine" factory-bean="processEngineConfiguration" factory-method="buildProcessEngine" />这个是Spring使用实例工厂方法实例化Bean的一种方式,工作作流引擎Flowable的各个组件就是使用此方式实例化的。由于使用工厂实例化的Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该FactoryBean的getObject方法所返回的对象。因此我们需要使用Spring Boot的方式来实例化Flowable的组件。有两种解决方法:
自定义FactoryBean,实现FactoryBean中的getObject这个方法,返回ProcessEngine对象使用ProcessEngineFactoryBean,推荐使用这种方式,以下为ProcessEngineFactoryBean源码。 public class ProcessEngineFactoryBean implements FactoryBean<ProcessEngine> ..{ ... protected ProcessEngineImpl processEngine; public ProcessEngine getObject() throws Exception { initializeExpressionManager(); initializeTransactionExternallyManaged(); if (processEngineConfiguration.getBeans()==null) { processEngineConfiguration.setBeans(new SpringBeanFactoryProxyMap(applicationContext)); } processEngine = (ProcessEngineImpl) processEngineConfiguration.buildProcessEngine(); return processEngine; } ... }ProcessEngineFactoryBean最终返回的是processEngine对象,repositoryService、runtimeService、formService等等组件都是通过processEngine里的getXX方法获得的。
通过WorkflowConfiguration就可以取代xml配置文件了。
在我们需要继承flowable-restApi的时候发现需要进行权限校验,我们可以通过下面的注解去掉security权限校验,在启动类上加上下面的注解:
@SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class, org.flowable.spring.boot.SecurityAutoConfiguration.class})