您的位置:首页 > 编程语言 > Java开发

Java Activiti(11)---整合到springMVC项目的详细配置

2017-10-15 10:05 555 查看

一、添加所需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&useUnicode=true&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入门

—————————————————————————————————————————————————–
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java spring mvc jar
相关文章推荐