1. 程式人生 > >配置activiti工作流

配置activiti工作流

activiti工作流在日常專案中應用很是廣泛 通過工作流來管理流程讓專案流程更加便於操作

設計好工作流程圖後我們需要在專案中配置好對應的配置檔案,可以在spring配置檔案中配置也可以單獨配置再引入spring配置檔案中下面我會將兩種方式都展現出來

工作流程圖需要在eclipse裡安裝外掛 在我的部落格中有activiti工作流的外掛 但是需要1個下載幣  

第一種 在單獨的配置檔案中配置

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
  	
  	<!--配置activiti  -->
  	<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
  		<!-- 配置資料來源 -->
  		<property name="dataSource" ref="dataSource"></property>
  		<!--配置事務管理器  -->
  		<property name="transactionManager" ref="txManager"></property>
  		<!--是否檢查資料庫有這些表  如果不存在自動剪表-->
  		<property name="databaseSchemaUpdate" value="true" />
  	</bean>
  	<!--建立流程工廠引擎物件  主要是用於實現服務介面  -->
  	<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
  		<property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
  	</bean>
  	
  	<!--通過工廠定義出服務介面  -->
  	<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService">
	</bean>
	<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService">
	</bean>
	<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService">
	</bean>
	<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService">
	</bean>
	<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"></bean>
</beans>

通過檢視原始碼你會發現它其中有一個屬性的名字就是processEngineConfiguration所以用它作為id再好不過了在該配置裡我們需要配置資料來源這個資料來源與spring檔案中的資料來源是同一個資料來源 當你把該配置檔案引入之後就可以使用它裡面的相關屬性了也要配置事務管理器 一般來說我們都會講databaseSchemaUpdate設定為true 這樣當我們資料庫中沒有這23張表(不同版本數量可能不同 用法功能一樣 不影響使用)它會自動給我們創建出來。

第二步引入工廠模式 這樣更加符合我們開發習慣 同時將processEngineConfiguration注入進來

第三步定義出服務介面  這些介面在原始碼裡已經寫好了 但沒有被spring管理起來 我們需要通過工廠模式將它引入進來被管理起來 就能做到事務統一。

第二種在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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop 
       					   http://www.springframework.org/schema/aop/spring-aop.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.xsd">
   <!-- 載入jdbc屬性檔案 -->
   <context:property-placeholder location="classpath:db.properties"/> 
   
  
   <!--1配置資料來源  -->
   
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   		<property name="driverClass" value="${db.driver}"/>
   		<property name="jdbcUrl" value="${db.url}"/>
   		<property name="user" value="${db.username}"/>
   		<property name="password" value="${db.password}"/>
   </bean>			 
    <!--2spring框架整合用於hibernate的工廠bean  -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"/>
    	<!--注入hibernate相關的屬性  -->
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
    		</props>
    	</property>
    	<!--注入hibernate的對映檔案 -->
    	<property name="mappingDirectoryLocations">
    		<list>
    			<value>classpath:com/cb/bos/domain</value>
    		</list>
    	</property>
    	 
    	
    </bean> 					 
    <!--3事物管理器  -->  
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	<property name="sessionFactory" ref="sessionFactory"/>
    </bean> 					 
     <!--元件掃描  -->
    	<context:component-scan base-package="com.cb.bos"/>
     <!--引入註解解析器  -->
     <context:annotation-config/>
     <!--事物註解   如果事物管理id為transactionManager則可以不用配置transaction-manager 因為預設為這個名字-->		 
     <tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 配置遠端服務的代理物件 -->
	<bean id="customerService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!--注入介面型別  -->
		<property name="serviceInterface" value="com.cb.bos.crm.CustomerService"/>
		<!--服務訪問路徑  -->
		<property name="serviceUrl" value="http://172.31.13.7:8080/crm/remoting/customer"/>
	</bean>

<!--配置一個工廠bean 用於建立shiro框架用到的過濾器  -->

	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!--注入安全管理器  -->
		<property name="securityManager" ref="securityManager"></property>
		<!--注入當前系統的登陸頁面  -->
		<property name="loginUrl" value="/login.jsp"/>
		<!--註冊成功跳轉頁面  -->
		<property name="successUrl" value="/index.jsp"/>
		<!--注入許可權不足提示頁面  -->
		<property name="unauthorizedUrl" value="/unauthorizedUrl.html"/>
		<!-- 注入url 攔截規則
			anon 表示可以匿名訪問
			validatecode.jsp* 後面加* 用於頁面快取後面跟的引數
			authc許可權認證
		 -->
		<property name="filterChainDefinitions">
			<value>
				/css/** = anon
				/js/** = anon
				/images/** = anon
				/validatecode.jsp* = anon 
				/login.jsp* = anon
				/userAction_login.action = anon
				/page_base_staff.action = perms["staff"]
				/* = authc
			</value>
		</property>
	</bean>
	
	<!--註冊自定義realm  -->
	<bean id="bosRealm" class="com.cb.bos.shiro.BOSRealm"></bean>
	<!--安全管理器  -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<!--注入上面的reaml  -->
		<property name="realm" ref="bosRealm"></property>
		<!--注入快取器  -->
		<property name="cacheManager" ref="cacheManager"></property>
	</bean>
	<!--註冊快取管理器  匯入架包  將ehcache.xml檔案匯入 該檔案通用  -->
	<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
	</bean>
	
	
	<bean id="defaultAdvisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
		<!-- 強制使用cglib為Action建立代理物件 -->
		<property name="proxyTargetClass" value="true"/>
	</bean>

	<!-- 切面類 -->
	<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>

	<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 id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
		<property name="processEngineConfiguration" ref="processEngineConfiguration" />
	</bean>
	
	<!-- 註冊Service -->
	<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService">
	</bean>
	<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService">
	</bean>
	<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService">
	</bean>
	<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService">
	</bean>
	<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"></bean>

</beans>

裡面有許多是其他的配置  大家只需要看activiti的 大部分配置你可以直接貼上過去用 只是命名得一樣。如果你有更好的方法請告訴我 留言或者其他