最近基於最新的Activiti7
配置了SpringBoot2
。
簡單上手使用了一番。發現市面上解決Activiti7
的教程不多,採坑也比較多,在Activiti6
配置數據源和Activiti7
有所區別,基於Activiti6
在Activiti7
裏是沒法正常使用的。接下來讓咱們看下區別。mysql
Activiti6
多數據源配置6的配置比較簡單點。git
# activiti 數據源 spring.datasource.activiti.driver=com.mysql.jdbc.Driver spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true spring.datasource.activiti.username=root spring.datasource.activiti.password=Rtqw123OpnmER spring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver
@ConfigurationProperties
加載以spring.datasource.activiti
開頭的DataSource
。ActivitiConfig
繼承AbstractProcessEngineAutoConfiguration
方法注入SpringProcessEngineConfiguration
bean,調用AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration
方法把建立的數據源注入。@Configuration public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.activiti") public DataSource activitiDataSource() { return DataSourceBuilder.create().build(); } @Bean public SpringProcessEngineConfiguration springProcessEngineConfiguration( PlatformTransactionManager transactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException { return baseSpringProcessEngineConfiguration( activitiDataSource(), transactionManager, springAsyncExecutor); } }
Activiti6
的數據源已經配置完成,若是是7的話會發現AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration
方法已經不存在了,咱們須要產考源碼構建方式改造一番。github
Activiti7
多數據源配置配置仍是要上面的。spring
ActivitiConfig
繼承AbstractProcessEngineAutoConfiguration
方法注入SpringProcessEngineConfiguration
bean。@ConfigurationProperties
加載以spring.datasource.activiti
開頭的DataSource
@Configuration public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.activiti") public DataSource activitiDataSource() { return DataSourceBuilder.create().build(); } ...略 }
SpringProcessEngineConfiguration
注入方式改成下面:@Bean @Primary public SpringProcessEngineConfiguration springProcessEngineConfiguration( PlatformTransactionManager transactionManager, SpringAsyncExecutor springAsyncExecutor, ActivitiProperties activitiProperties, ProcessDefinitionResourceFinder processDefinitionResourceFinder, @Autowired(required = false) DefaultActivityBehaviorFactoryMappingConfigurer processEngineConfigurationConfigurer, @Autowired(required = false) List<ProcessEngineConfigurator> processEngineConfigurators, UserGroupManager userGroupManager, DataSource dataSource) throws IOException { SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration(); conf.setConfigurators(processEngineConfigurators); configureProcessDefinitionResources(processDefinitionResourceFinder, conf); conf.setDataSource(dataSource); conf.setTransactionManager(transactionManager); if (springAsyncExecutor != null) { conf.setAsyncExecutor(springAsyncExecutor); } conf.setDeploymentName(activitiProperties.getDeploymentName()); conf.setDatabaseSchema(activitiProperties.getDatabaseSchema()); conf.setDatabaseSchemaUpdate(activitiProperties.getDatabaseSchemaUpdate()); conf.setDbHistoryUsed(activitiProperties.isDbHistoryUsed()); conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate()); if (!activitiProperties.isAsyncExecutorActivate()) { ValidatorSet springBootStarterValidatorSet = new ValidatorSet("activiti-spring-boot-starter"); springBootStarterValidatorSet.addValidator(new AsyncPropertyValidator()); if (conf.getProcessValidator() == null) { ProcessValidatorImpl processValidator = new ProcessValidatorImpl(); processValidator.addValidatorSet(springBootStarterValidatorSet); conf.setProcessValidator(processValidator); } else { conf.getProcessValidator().getValidatorSets().add(springBootStarterValidatorSet); } } conf.setMailServerHost(activitiProperties.getMailServerHost()); conf.setMailServerPort(activitiProperties.getMailServerPort()); conf.setMailServerUsername(activitiProperties.getMailServerUserName()); conf.setMailServerPassword(activitiProperties.getMailServerPassword()); conf.setMailServerDefaultFrom(activitiProperties.getMailServerDefaultFrom()); conf.setMailServerUseSSL(activitiProperties.isMailServerUseSsl()); conf.setMailServerUseTLS(activitiProperties.isMailServerUseTls()); if (userGroupManager != null) { conf.setUserGroupManager(userGroupManager); } conf.setHistoryLevel(activitiProperties.getHistoryLevel()); conf.setCopyVariablesToLocalForTasks(activitiProperties.isCopyVariablesToLocalForTasks()); conf.setSerializePOJOsInVariablesToJson(activitiProperties.isSerializePOJOsInVariablesToJson()); conf.setJavaClassFieldForJackson(activitiProperties.getJavaClassFieldForJackson()); if (activitiProperties.getCustomMybatisMappers() != null) { conf.setCustomMybatisMappers( getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers())); } if (activitiProperties.getCustomMybatisXMLMappers() != null) { conf.setCustomMybatisXMLMappers( new HashSet<>(activitiProperties.getCustomMybatisXMLMappers())); } if (activitiProperties.getCustomMybatisXMLMappers() != null) { conf.setCustomMybatisXMLMappers( new HashSet<>(activitiProperties.getCustomMybatisXMLMappers())); } if (activitiProperties.isUseStrongUuids()) { conf.setIdGenerator(new StrongUuidGenerator()); } if (activitiProperties.getDeploymentMode() != null) { conf.setDeploymentMode(activitiProperties.getDeploymentMode()); } conf.setActivityBehaviorFactory(new DefaultActivityBehaviorFactory()); if (processEngineConfigurationConfigurer != null) { processEngineConfigurationConfigurer.configure(conf); } return conf; } private void configureProcessDefinitionResources( ProcessDefinitionResourceFinder processDefinitionResourceFinder, SpringProcessEngineConfiguration conf) throws IOException { List<Resource> procDefResources = processDefinitionResourceFinder .discoverProcessDefinitionResources(); if (!procDefResources.isEmpty()) { conf.setDeploymentResources(procDefResources.toArray(new Resource[0])); } }
在以上配置中可能會有jdbcUrl is required with driverClassName
錯誤sql
解決辦法以下:app
# activiti 數據源 spring.datasource.activiti.driver=com.mysql.jdbc.Driver spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true # url換爲jdbc-url 解決jdbcUrl is required with driverClassName錯誤 # 官方文檔的解釋是: # 由於鏈接池的實際類型沒有被公開,因此在您的自定義數據源的元數據中沒有生成密鑰,並且在IDE中沒有完成(由於DataSource接口沒有暴露屬性)。 # 另外,若是您碰巧在類路徑上有Hikari,那麼這個基本設置就不起做用了,由於Hikari沒有url屬性(可是確實有一個jdbcUrl屬性)。在這種狀況下,您必須重寫您的配置以下: spring.datasource.activiti.jdbc-url=${spring.datasource.activiti.url} spring.datasource.activiti.username=root spring.datasource.activiti.password=Rtqw123OpnmER spring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver
Activiti7
國內的教程不是不少,須要本身在社區裏或者官方文檔,源碼去看了解,細心學習。spring-boot
示例代碼地址: activiti-starter做者GitHub:
Purgeyao 歡迎關注學習