ssm和activiti工作流的集成

一:导入activiti jar包

ssm和activiti工作流的集成

二:添加Activiti配置文件-applicationContext-activiti.xml-配置核心对象

<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="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/Activiti-CRM" />
    <property name="username" value="root" />
    <property name="password" value="1111" />
  </bean>
 
   <!-- 核心服务对象工厂bean配置 -->
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <!-- 自动建表 -->
    <property name="databaseSchemaUpdate" value="true" />
  </bean>
 <!-- 核心服务对象工厂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="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
		
 </beans> 

三:把Activiti配置文件-applicationContext-activiti.xml集成到Spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 1、引入外部属性配置 -->
	<context:property-placeholder location="classpath:db.properties" />
	
	<!-- 2、数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${db.driver}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>
	<!-- 3、Mybatis的核心配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" /> 
		<!-- mybatis核心配置 
		<property name="configLocation" value="classpath:mybatis-config.xml" /> 
		-->
		<!-- 对象类型别名 -->
		<property name="typeAliasesPackage" value="cn.itsource.crm.domain,cn.itsource.crm.query"/>
		<!-- 设置映射文件地址 -->
		<property name="mapperLocations" value="classpath:cn/itsource/crm/mapper/*Mapper.xml">
		</property>
	</bean> 
	
	<!-- 4、引入外部配置文件 -->
	<import resource="classpath:applicationContext-*.xml"/>
	
	<!-- 
	 5、 事务配置
          1)事务管理器
          2)切面
          3)通知
	 -->
     <!--事务管理器 -->
	 <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 
    	切面
    	cn.itsource.crm.service..*.*(..)
    	..*    : 匹配在之前目录下的所有资源(包括子或子的子)
    	最后个.* :匹配类的方法,代表所有方法的意思    
    	(..)   : 方法上的任意参数     
     -->
    <aop:config>
        <aop:pointcut id="crmPointcut"
                expression="execution(* cn.itsource.crm.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="crmPointcut"/>
    </aop:config>
    <!-- 
    		通知
     -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <!-- 除了查询以外,都需要开启事务 -->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 日志相关 -->
    <bean id="logUtils" class="cn.itsource.crm.utils.SystemLogUtils">
    	<property name="logService" ref="systemLogServiceImpl"></property>
    </bean>
    <aop:config>
    	<!-- 怎么切 -->
    	<aop:pointcut expression="execution(* cn.itsource.crm.service..*.*(..))" id="logPointcut"/>
    	<!-- 该做什么 -->
    	<aop:aspect ref="logUtils">
    		<aop:after method="writeLog" pointcut-ref="logPointcut"/>
    	</aop:aspect>
    </aop:config>
    
</beans>

四:测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Activiti_Test {

	@Autowired
	ProcessEngine processEngine;
	
	@Test
	public void testAct() throws Exception {
		System.out.println(processEngine);
	}
}