在第一家公司工作的时候做的第一个项目主要负责开发营运审批系统,因为有过其他工作引擎的开发基础,但由于是收费的,所以在在项目调研阶段我先搜索资料选择使用哪个开源工作流引擎,最终一致确定了spingMVC+spring+mybatis整合Activiti工作流,今天来整理一下,希望对大家有用,ssm整合网上很多,今天主要的内容是activit嵌入ssm当中,首先来说一下:
1. 我理解的工作流:
在工作中各个岗位都需要有人把控业务流程,就像流程控制语言一样,一步一步都对应的不同的业务,但整体串联起来就是一个完整的业务。而且实际工作中尤其是在企业内部系统的管理中,确实需要对应许多审批流程,以此便利内部员工的工作流程清晰,便利
而工作流就是能够在程序中,将这些支离破碎的流程,通过配置的方式管理起来,整体作为一个流程,方便修改,也方便维护。
2. 什么是Activiti:
Activiti是一个比较出名的框架,或者说就是一个工作流引擎,通俗的说,就是Activiti引擎我们只要按照它已有的配置,来进行现有业务的对应,它就能够自动帮助我们完成以前不好控制的流程问题.
第一步:配置db.propertise文件,用于配置数据源
#oracle
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=admin
jdbc.password=1234
##oracle loacl
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc.username=admin
#jdbc.password=1234
第二步:配置spring相关文件,取名为dispatcher-servlet.xml,放在WEB-INF目录下,详见配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源1 -->
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="30" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="10" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<!-- myBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 引入mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations">
<list>
<value>classpath*:com/hd/demo/**/mapper/*Mapper.xml</value>
<value>classpath*:com/hd/demo/**/**/mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hd.demo.*.dao,com.cxq.demo.*.*.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.hd.demo.*.service..*.*(..)) ||execution(* com.hd.demo.*.*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
<!-- springMVC配置 -->
<mvc:annotation-driven />
<mvc:interceptors>
<bean class="com.linkstec.lmsp.spring.LHandlerInterceptor" />
<bean class="com.cxq.xyzq.base.LoginInterceptor" >
<property name="uncheckUrls">
<list>
<value>/downloadFile</value>
</list>
</property>
</bean>
</mvc:interceptors>
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</bean>
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="514572800"/>
<property name="maxInMemorySize" value="4096" />
</bean>
<!-- 导入Activiti配置 -->
<import resource="classpath:activiti.cfg.xml"/>
</beans>
第三步:配置web.xml
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml,/WEB-INF/springmvc-quartz.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
第四步:去官网下载Activiti对应的核心jar包Activiti官网
或者从我这边下载 链接: https://pan.baidu.com/s/1PB6-p968n1UYpf4zGEGmyA 密码: ueh9
下载下来一系列jar包,但我们不需要全部,我们只需要在WEB-INF下面的lib目录下引入以下jar即可
第五步:配置activiti,建立一个xml文件 activiti.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 定义流程引擎配置 -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
</bean>
<!-- 定义流程引擎 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<!-- 定义Service服务 -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<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" />
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
<bean id="formService" factory-bean="processEngine" factory-method="getFormService"></bean>
</beans>
dataSource即是数据库连接配置
<!-- 配置数据源1 -->
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="30" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="10" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<property name="databaseSchemaUpdate" value="true" />
databaseSchemaUpdate:在工作流引擎启动和关闭的使用数据库的针对表结构的处理策略。
默认为false:在工作流引擎启动时检查数据库脚本的版本和activiti library的版本是否一致如果不一致抛出异常信息。
在脚本结构发生变化时候,检查表结构是否存在,如果存在则更新,如果不存在则创建。
当工作流引擎被创建时候创建,当工作流引擎关闭时删除表结构信息。
<property name="jobExecutorActivate" value="false" />
JobExecutor是一个管理一系列激活timer和异步消息的线程的组件。在单元测试里可以使用ManagementService.createJobQuery查询线程,使用ManagementService.executeJob执行线程的方法。
默认情况下JobExecutor在流程引擎启动的时候激活状态。可以通过以下方式关闭。
<property name="transactionManager" ref="transactionManager" />
即将Activiti交给spring事务管理
第六步:将activiti.cfg.xml引入spring配置文件中
<!-- 导入Activiti配置 -->
<import resource="classpath:activiti.cfg.xml"/>
spingMVC+spring+mybatis整合activiti工作流引擎大功告成