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中要添加相應的配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM