Java Activiti(11)---整合到springMVC項目的詳細配置

一、添加所需jar包

從官網上下載對應的版本,在裏面找到Activiti所需jar包,這裏我添加了Activiti提供的11個jar,分別爲:

activiti-bpmn-converter-5.16.4.jar
activiti-bpmn-layout-5.16.4.jar
activiti-bpmn-model-5.16.4.jar
activiti-common-rest-5.16.4.jar
activiti-engine-5.16.4.jar
activiti-image-generator-5.16.4.jar
activiti-json-converter-5.16.4.jar
activiti-process-validation-5.16.4.jar
activiti-rest-5.16.4.jar
activiti-simple-workflow-5.16.4.jar
activiti-spring-5.16.4.jar

同時也需要如下5個jar包,

mybatis-3.2.5.jar
slf4j-api-1.7.6.jar
slf4j-log4j12-1.7.6.jar
commons-lang3-3.3.2.jar
joda-time-2.1.jar

二、添加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:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    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/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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    ">
    <!-- 開啓註解掃描 -->
    <context:annotation-config />
    <!-- 自動註冊實體bean -->
    <context:component-scan base-package="com.cloud.wyscha">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <aop:aspectj-autoproxy proxy-target-class="true"/> 

    <context:property-placeholder location="classpath:conf/jdbc.properties,classpath:conf/config.properties" />

    <bean id="masterDS" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc_driver}" />
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
        <property name="initialSize" value="30" />
        <property name="maxActive" value="1000" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="10000" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <bean id="dataSource" class="com.cloud.common.db.datasource.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="masterDS" value-ref="masterDS" />
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="masterDS" />
    </bean>

    <!-- mybatis文件配置,掃描所有mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:conf/mybatis-config.xml"></property>
    </bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cloud.wyscha.*.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--基於註解的聲明式事務-->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 使用聲明式事務 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="call*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />

            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <bean id="dataSourceExchange" class="com.cloud.common.db.datasource.DataSourceExchange" />

    <aop:config>
        <aop:pointcut id="service1" expression="execution(* com.cloud.wyscha.*.service..*Service*.*(..))" />
        <aop:advisor advice-ref="dataSourceExchange" pointcut-ref="service1" order="1" />
        <aop:advisor advice-ref="txAdvice"  pointcut-ref="service1"  order="3" />
    </aop:config>

    <!--創建流程引擎對象-->
    <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="mailServerHost" value="localhost"/>
        <property name="mailServerPort" value="5025"/>
        <!-- 是否啓動jobExecutor -->
        <!-- <property name="jobExecutorActivate"value="false" /> -->
    </bean>

    <!--流程引擎配置-->
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>

    <!-- 創建activiti提供的各種服務 -->
    <!-- 工作流倉儲服務 -->
    <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="formService" factory-bean="processEngine" factory-method="getFormService" />
    <!-- 工作流唯一服務 -->
    <bean id="IdentityService" factory-bean="processEngine" factory-method="getIdentityService"/>
</beans>

三、創建activiti數據庫

從官網下載並解壓的文件中找到database文件下的各類數據庫腳本,這裏用的是mysql數據庫,所以把其中mysql的腳本拷貝出來,新建一個mysql數據庫,名爲activiti,分別運行

activiti.mysql.create.engine.sql
activiti.mysql.create.identity.sql
activiti.mysql.create.history.sql

這三個sql腳本,它們分別創建了12張、4張、8張表,共24張表格,請覈對是否全部創建成功。
注:可能是因爲mysql版本或者其它原因,創建datetime格式的字段時不能有長度定義,所以需要把上述三個sql腳本中的datetime(3)改爲datetime,另外把activiti.mysql.create.engine.sql中的CURRENT_TIMESTAMP(3)改爲CURRENT_TIMESTAMP

四、單元測試

public static void main(String[] args) {
        // 1.創建Activiti配置對象的實例
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration();
        // 2.設置數據庫連接信息

        // 設置數據庫地址
        configuration
                .setJdbcUrl("jdbc:mysql://localhost:3306/activiti?createDatabaseIfNotExist&amp;useUnicode=true&amp;characterEncoding=utf8");
        // 數據庫驅動
        configuration.setJdbcDriver("com.mysql.jdbc.Driver");
        // 用戶名
        configuration.setJdbcUsername("root");
        // 密碼
        configuration.setJdbcPassword("");

        // 設置數據庫建表策略
        /**
         * DB_SCHEMA_UPDATE_TRUE:如果不存在表就創建表,存在就直接使用
         * DB_SCHEMA_UPDATE_FALSE:如果不存在表就拋出異常
         * DB_SCHEMA_UPDATE_CREATE_DROP:每次都先刪除表,再創建新的表
         */
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        // 3.使用配置對象創建流程引擎實例(檢查數據庫連接等環境信息是否正確)
        ProcessEngine processEngine = configuration.buildProcessEngine();

        System.out.println(processEngine);
    }

—————————————————————————————————————————————————–

java架構師項目實戰,高併發集羣分佈式,大數據高可用視頻教程,共760G

下載地址:

https://item.taobao.com/item.htm?id=555888526201

01.高級架構師四十二個階段高
02.Java高級系統培訓架構課程148課時
03.Java高級互聯網架構師課程
04.Java互聯網架構Netty、Nio、Mina等-視頻教程
05.Java高級架構設計2016整理-視頻教程
06.架構師基礎、高級片
07.Java架構師必修linux運維繫列課程
08.Java高級系統培訓架構課程116課時
+
hadoop系列教程,java設計模式與數據結構, Spring Cloud微服務, SpringBoot入門

—————————————————————————————————————————————————–

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章