轉載請注明出處 https://www.cnblogs.com/rolayblog/p/10444866.html
背景
某天有同事反映,審批記錄中看到該他審批,但是待辦里面卻沒有待辦事項,於是看服務器日志,找到了問題所在。業務代碼報錯,已經拋出了異常,但是流程還是繼續往下走了,完全不受@Transactional注解影響,看到這里就知道問題在哪里了,此時我用的正是默認獲取流程引擎對象的方式,Activiti大佬和Spring大佬自己做自己的,不行要把它們掰彎,強行在一起。於是翻閱《Activiti 權威指南》找到了解決方法,買了這本書就沒有怎么看過,只是遇到問題了才翻一翻。另外,感謝分享牛大佬的提醒。
Tip
了解activiti的道友們應該都知道,流程引擎的配置文件放在resources下,可以直接通過ProcessEngines.getDefaultProcessEngine()方法直接獲取流程引擎對象,亦或者通過ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream()讀取其他位置的配置文件,但是呢,咋們既然用了強大的Sring,像事務、流程引擎對象的管理甚至流程引擎內部的API都交給統統交給它吧!進入正題吧。
Maven
我這里排除了spring-context,因為項目里面用的spring是高版本的,但是Activiti 5.22只能引用這個版本的activiti-spring,可根據自己需要刪除。
1 <dependency> 2 <groupId>org.activiti</groupId> 3 <artifactId>activiti-spring</artifactId> 4 <version>5.22.0</version> 5 <exclusions> 6 <exclusion> 7 <groupId>org.springframework</groupId> 8 <artifactId>spring-context</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency>
流程引擎配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 5 <!--配置數據源--> 6 <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> 7 <property name="driverClass" value="com.mysql.jdbc.Driver"/> 8 <property name="url" value="jdbc:mysql://數據庫地址"/> 9 <property name="username" value="賬號"/> 10 <property name="password" value="密碼"/> 11 </bean> 12 <!--流程引擎配置--> 13 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 14 <property name="dataSource" ref="dataSource"/> 15 <property name="databaseSchemaUpdate" value="false"/> 16 </bean> 17 </beans>
Spring 配置文件
這里面加入了流程引擎,和幾個Service。
1 <!--Spring流程引擎配置--> 2 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 3 <property name="dataSource" ref="dataSource"/> 4 <property name="databaseSchemaUpdate" value="false"/> 5 <property name="transactionManager" ref="transactionManager"/> 6 <property name="idGenerator" ref="uuidGenerator"/> 7 </bean> 8 <!--自定義ID生成--> 9 <bean id="uuidGenerator" class=""/> 10 <!--Spring流程引擎--> 11 <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> 12 <property name="processEngineConfiguration" ref="processEngineConfiguration"/> 13 </bean> 14 <!--流程引擎服務--> 15 <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/> 16 <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/> 17 <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/> 18 <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/> 19 <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
至此就配置完成了,可以在代碼里面通過@Autowired獲取流程引擎或者API了,事務也統一管理了。
