activiti搭建(二)与Spring集成

  转载请注明源地址:http://www.cnblogs.com/lighten/p/5876773.html

  本文主要讲解如何将Activiti和Spring框架集成,再过一段时间将会将一个基础的demo放在github上,地址将会在activiti系列导读文章中给出,如果未给出,则是因为没有构建完成(学习中,更新较慢)。按照搭建篇章的顺序,也能搭建起来。

  上一章将了数据库的初始化,继续那章建立的maven项目。Activiti与Spring集成需要相应的jar包,下面将补充其他maven依赖:

		<dependency>
			<groupId>org.activiti</groupId>
			<artifactId>activiti-spring</artifactId>
			<version>5.21.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.5.RELEASE</version>
		</dependency>

  先配置一下数据源,在src/main/resources下创建spring-db.xml文件,此文件用于配置数据库相关事项:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

	<!-- 数据库配置文件 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${driverClassName}"/>
		<property name="url" value="${url}"/>
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
	</bean>
	
	<!-- 数据库事务管理 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>
</beans>

  这里将数据库的相关参数放入了文件db.properties中,并在xml中引用了,这样方便修改,路径依旧是classpath下,也可以改,相应的配置路径也做修改即可。文件内容如下:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/activiti?useSSL=false
username=root
password=root

  在src/main/resources下创建spring-activiti.xml文件,此文件用于activiti的相关配置(当然也可以选择使用代码加注解的方式进行配置),里面内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

	<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
		<property name="dataSource"><ref bean="dataSource"/></property>
		<property name="transactionManager"><ref bean="transactionManager"/></property>
		<property name="databaseType" value="mysql"></property>
		<property name="databaseSchemaUpdate" value="false"></property>
	</bean>
	
	<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
		<property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
	</bean>
	
	<!-- 7种服务 不一定全部使用 -->
	<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"/>
</beans>

  最后在classpath创建一个spring-app.xml,用于全局配置,将所有配置汇总。里面内容目前如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

	<!-- 汇总配置 -->
	<import resource="classpath:spring-db.xml"/>
	<import resource="classpath:spring-activiti.xml"/>
</beans>

  最后在src/main/webapp/WEB-INF/web.xml中配置相关项:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">

	<display-name>Archetype Created Web Application</display-name>

	<context-param>
		<description>Spring全局配置</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-app.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

  然后就能在tomcat中跑起来了。这只是一种最基本的配置方式,更详细的配置和相关说明会在之后一一补充讲解。数据源和事务管理可以改为自己需要的配置,也可以使用orm或者jpa,这个在id为processEngineConfiguration的bean中要添加相应的配置。

posted @ 2016-09-16 22:54  dark_saber  阅读(4840)  评论(0编辑  收藏  举报