springboot2.0整合activiti6.0
阿新 • • 發佈:2019-01-21
activiti6.0直接使用starter整合到springboot2.0時,會報異常,無法直接進行整合。所以我才用spring-activiti來整合activiti6.0.
整體檔案結構如下:
1.pom檔案依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.stu</groupId> <artifactId>activitiApplication</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!-- 資料庫相關 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.19</version> </dependency> <!-- webmvc必須依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- activiti整合spring --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>6.0.0</version> </dependency> </dependencies> </project>
我採用的是mysql5.7.22,如果parent才用2.0.4版會導致連線資料庫出現問題,所有我才用2.0.3(也可以採用2.0.1或者2.0.2版本).
2.bpmn檔案配置springboot預設從classpath的processes檔案下讀取,所以必須建該資料夾。示例流程圖如下:
3.properties檔案配置:
server.port=8080 server.servlet.context-path=/activiti mybatis.mapper-locations=classpath:mapper/*.xml spring.datasource.url=jdbc:mysql://localhost:3306/stu spring.datasource.username = root spring.datasource.password = password spring.datasource.driver-class-name = com.mysql.jdbc.Driver
4.啟動類:
package com.stu; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.stu.mapper") public class ActivitiApplication { public static void main(String[] args) { SpringApplication.run(ActivitiApplication.class, args); } }
5.配置類:
package com.stu.config;
import java.io.IOException;
import javax.sql.DataSource;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
public class ActivitiConfuguration {
@Bean
public ProcessEngine processEngine(DataSourceTransactionManager transactionManager, DataSource dataSource) throws IOException {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
//自動部署已有的流程檔案
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn");
configuration.setTransactionManager(transactionManager);
configuration.setDataSource(dataSource);
configuration.setDatabaseSchemaUpdate("true");
configuration.setDeploymentResources(resources);
configuration.setDbIdentityUsed(false);
return configuration.buildProcessEngine();
}
@Bean
public RepositoryService repositoryService(ProcessEngine processEngine) {
return processEngine.getRepositoryService();
}
@Bean
public RuntimeService runtimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
@Bean
public TaskService taskService(ProcessEngine processEngine) {
return processEngine.getTaskService();
}
@Bean
public HistoryService historyService(ProcessEngine processEngine) {
return processEngine.getHistoryService();
}
@Bean
public ManagementService managementService(ProcessEngine processEngine) {
return processEngine.getManagementService();
}
@Bean
public IdentityService identityService(ProcessEngine processEngine) {
return processEngine.getIdentityService();
}
}
package com.stu.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidConfiguration {
@Autowired
Environment env;
/**
* 資料來源:配置DruidDataSource
* @return
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUrl(env.getProperty("spring.datasource.url"));
ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
ds.setPassword(env.getProperty("spring.datasource.password"));
ds.setUsername(env.getProperty("spring.datasource.username"));
ds.setInitialSize(2);//初始化時建立物理連線的個數
ds.setMaxActive(20);//最大連線池數量
ds.setMinIdle(0);//最小連線池數量
ds.setMaxWait(60000);//獲取連線時最大等待時間,單位毫秒。
ds.setValidationQuery("SELECT 1");//用來檢測連線是否有效的sql
ds.setTestOnBorrow(false);//申請連線時執行validationQuery檢測連線是否有效
ds.setTestWhileIdle(true);//建議配置為true,不影響效能,並且保證安全性。
ds.setPoolPreparedStatements(false);//是否快取preparedStatement,也就是PSCache
return ds;
}
/**
* 事務:配置DataSourceTransactionManager
* @param dataSource
* @return
*/
@Bean("dataSourceTransactionManager")
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
6.controller控制層測試:
package com.stu.controller;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ActivitiController {
@Autowired
private RepositoryService repositoryService;
@RequestMapping("/deploy")
public String deploy() {
Deployment deployment = repositoryService.createDeployment()
.name("demoDeployment")
.addClasspathResource("processes/Demo.bpmn")
.addClasspathResource("processes/Demo.png")
.deploy();
System.out.println("部署Id: " + deployment.getId());
System.out.println("部署名稱: " + deployment.getName());
return "部署成功";
}
}
其實,在程式啟動時springboot會自動部署。