企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
课件代码:https://gitee.com/flymini/codes01/tree/master/flowable_/learn-flowable02 **** 步骤如下: <br/> **1. 创建一个 maven 项目,`pom.xml`如下** ```xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <flowable.version>6.3.0</flowable.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> </properties> <dependencies> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-engine</artifactId> <version>${flowable.version}</version> </dependency> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-spring</artifactId> <version>${flowable.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.9</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.0.7.RELEASE</version> <scope>compile</scope> </dependency> </dependencies> ``` **2. `resources/log4j.properties` 日志配置文件** ```properties log4j.rootLogger=DEBUG, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n ``` **3. `resources/flowable-spring.xml` Flowable核心配置文件** ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/flowable_spring?useUnicode=true&amp;characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 生成流程图参数 --> <bean id="processDiagramGenerator" class="org.flowable.image.impl.DefaultProcessDiagramGenerator"/> <!-- 流程引擎配置类 --> <bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource"/> <property name="transactionManager" ref="transactionManager"/> <!--表处理策略: false (默认): 当引擎启动时,检查数据库表结构的版本是否匹配库文件版本。版本不匹配时抛出异常。 true:数据库中表存在则使用,没有则创建。 create-drop: 引擎创建时创建表结构,并在引擎关闭时删除表结构。 --> <property name="databaseSchemaUpdate" value="true"/> <property name="asyncExecutorActivate" value="false"/> <!-- 流程部署时自动生成对应的图片 --> <property name="createDiagramOnDeploy" value="true"/> <!-- 生成流程图参数 --> <property name="processDiagramGenerator" ref="processDiagramGenerator"/> <!-- 流程图字体 --> <property name="activityFontName" value="宋体"/> <property name="labelFontName" value="宋体"/> <property name="annotationFontName" value="宋体"/> </bean> <!-- 流程引擎--> <bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/> </bean> <!-- 各种服务接口 --> <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"/> <!-- 开启事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans> ``` **4. 可以拿到各种服务接口了** (1)如果是在测试单元中可以使用注解`@ContextConfiguration`引入配置,然后获取接口。 ```java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:flowable-spring.xml") public class FlowableTests { //获取各种服务接口 @Autowired private RepositoryService repositoryService; @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private HistoryService historyService; @Autowired private ManagementService managementService; @Test public void deploymentProcess() { //org.flowable.engine.impl.RepositoryServiceImpl@5b6e8f77 System.out.println(this.repositoryService); } } ``` (2)不是在测试单元中可以如下获取各种接口。 ```java public class FlowableTests { @Test public void deploymentProcess() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:flowable-spring.xml"); RepositoryService repositoryService =applicationContext.getBean(RepositoryService.class); //org.flowable.engine.impl.RepositoryServiceImpl@7fedfe27 System.out.println(repositoryService); } } ```