前面学习时,使用的内存数据库H2,实际使用时,一般会替换我们指定的数据库,这个时候要怎么配置呢?
1.查看activiti-spring-boot-starter-basic的spring.factories配置。
2.查看DataSourceProcessEngineAutoConfiguration定义。
注解不是本次学习的重点,关于@AutoConfigureAfter可以查看https://zhuanlan.zhihu.com/p/157683108
3.查看DataSourceAutoConfiguration定义。
4.查看DataSourceProperties定义。
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
private ClassLoader classLoader;
/**
* Whether to generate a random datasource name.
*/
private boolean generateUniqueName = true;
/**
* Datasource name to use if "generate-unique-name" is false. Defaults to "testdb"
* when using an embedded database, otherwise null.
*/
private String name;
/**
* Fully qualified name of the connection pool implementation to use. By default, it
* is auto-detected from the classpath.
*/
private Class<? extends DataSource> type;
/**
* Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
*/
private String driverClassName;
/**
* JDBC URL of the database.
*/
private String url;
/**
* Login username of the database.
*/
private String username;
/**
* Login password of the database.
*/
private String password;
/**
* JNDI location of the datasource. Class, url, username and password are ignored when
* set.
*/
private String jndiName;
//......
}
5.配置数据源的相关属性。
根据上面属性定义,我们在resources目录下创建application.yml文件。并配置相关属性。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/activiti_demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: root
password: **********
6.引入数据库驱动依赖。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
7.activiti流程引擎配置。
@ConfigurationProperties("spring.activiti")
public class ActivitiProperties {
private boolean checkProcessDefinitions = true;
private boolean jobExecutorActivate = false;
private boolean asyncExecutorEnabled = true;
private boolean asyncExecutorActivate = true;
private boolean restApiEnabled;
private String deploymentName;
private String mailServerHost = "localhost";
private int mailServerPort = 1025;
private String mailServerUserName;
private String mailServerPassword;
private String mailServerDefaultFrom;
private boolean mailServerUseSsl;
private boolean mailServerUseTls;
private String databaseSchemaUpdate = "true";
private String databaseSchema;
protected boolean isDbIdentityUsed = true;
protected boolean isDbHistoryUsed = true;
private HistoryLevel historyLevel = HistoryLevel.AUDIT;
private String processDefinitionLocationPrefix = "classpath:/processes/";
private List<String> processDefinitionLocationSuffixes = Arrays.asList("**.bpmn20.xml", "**.bpmn");
private String restApiMapping = "/api/*";
private String restApiServletName = "activitiRestApi";
private boolean jpaEnabled = true; // true by default
private List<String> customMybatisMappers;
private List<String> customMybatisXMLMappers;
//.......
}
8.配置activiti流程引擎。
一般默认就好了,有需要可以根据上面属性进行配置。
9.运行测试。
运行测试后正常。之前H2数据库使用的内存数据库,每次启动都会重新创建数据库和表,改为mysql后,只有首次运行时会自动创建25张表,之后每次启动都会启动一个twoTaskProcess流程的实例。