activiti搭建(二)与Spring集成
转载请注明源地址:http://www.cnblogs.com/lighten/p/5876773.html
本文主要讲解如何将Activiti和Spring框架集成,再过一段时间将会将一个基础的demo放在github上,地址将会在activiti系列导读文章中给出,如果未给出,则是因为没有构建完成(学习中,更新较慢)。按照搭建篇章的顺序,也能搭建起来。
上一章将了数据库的初始化,继续那章建立的maven项目。Activiti与Spring集成需要相应的jar包,下面将补充其他maven依赖:
1 2 3 4 5 6 7 8 9 10 11 | <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文件,此文件用于配置数据库相关事项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?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下,也可以改,相应的配置路径也做修改即可。文件内容如下:
1 2 3 4 | driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql: //localhost:3306/activiti?useSSL=false username=root password=root |
在src/main/resources下创建spring-activiti.xml文件,此文件用于activiti的相关配置(当然也可以选择使用代码加注解的方式进行配置),里面内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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,用于全局配置,将所有配置汇总。里面内容目前如下:
1 2 3 4 5 6 7 8 9 10 | <?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中配置相关项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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中要添加相应的配置。
分类:
activiti系列
标签:
activiti相关
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法