springboot 集成activiti5
时间: 2023-11-09 13:49:29 浏览: 92
要在 Spring Boot 中集成 Activiti 5,需要按照以下步骤进行操作:
1. 添加依赖项
在 pom.xml 文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-compatibility</artifactId>
<version>5.22.0</version>
</dependency>
```
2. 配置数据源
在 application.properties 文件中配置数据源:
```
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
```
3. 添加 Activiti 配置
创建一个类 ActivitiConfig,添加以下内容:
```java
@Configuration
public class ActivitiConfig {
@Autowired
private DataSource dataSource;
@Bean
public ProcessEngineConfiguration processEngineConfiguration() {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(dataSource);
configuration.setDatabaseSchemaUpdate("true");
configuration.setAsyncExecutorActivate(false);
return configuration;
}
@Bean
public ProcessEngineFactoryBean processEngine() {
ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
return factoryBean;
}
@Bean
public RepositoryService repositoryService() throws Exception {
return processEngine().getObject().getRepositoryService();
}
@Bean
public RuntimeService runtimeService() throws Exception {
return processEngine().getObject().getRuntimeService();
}
@Bean
public TaskService taskService() throws Exception {
return processEngine().getObject().getTaskService();
}
@Bean
public HistoryService historyService() throws Exception {
return processEngine().getObject().getHistoryService();
}
@Bean
public ManagementService managementService() throws Exception {
return processEngine().getObject().getManagementService();
}
@Bean
public IdentityService identityService() throws Exception {
return processEngine().getObject().getIdentityService();
}
}
```
4. 测试
在控制器中添加以下内容:
```java
@RestController
public class ActivitiController {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@GetMapping("/start")
public String startProcess() {
Map<String, Object> variables = new HashMap<>();
variables.put("name", "Activiti");
runtimeService.startProcessInstanceByKey("myProcess", variables);
return "Process started!";
}
@GetMapping("/complete")
public String completeTask() {
Task task = taskService.createTaskQuery().singleResult();
taskService.complete(task.getId());
return "Task completed!";
}
}
```
启动应用程序并访问 http://localhost:8080/start,然后访问 http://localhost:8080/complete。您应该会看到以下输出:
```
Process started!
Task completed!
```
这意味着 Activiti 流程已经成功地启动并完成了任务。
阅读全文