解决Springboot2.1.x配置Activiti7单独数据源问题
1|1简介
最近基于最新的Activiti7配置了SpringBoot2。
简单上手使用了一番。发现市面上解决Activiti7的教程很少,采坑也比较多,在Activiti6配置数据源和Activiti7有所区别,基于Activiti6在Activiti7里是无法正常使用的。接下来让我们看下区别。
1|2问题
Activiti6多数据源配置
6的配置比较简单点。
先加入配置:
#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方法注入SpringProcessEngineConfigurationbean,调用AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration方法把创建的数据源注入。
@Configuration publicclassActivitiConfigextendsAbstractProcessEngineAutoConfiguration{ @Bean @ConfigurationProperties(prefix="spring.datasource.activiti") publicDataSourceactivitiDataSource(){ returnDataSourceBuilder.create().build(); } @Bean publicSpringProcessEngineConfigurationspringProcessEngineConfiguration( PlatformTransactionManagertransactionManager, SpringAsyncExecutorspringAsyncExecutor)throwsIOException{ returnbaseSpringProcessEngineConfiguration( activitiDataSource(), transactionManager, springAsyncExecutor); } }
Activiti6的数据源已经配置完成,如果是7的话会发现
AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration方法已经不存在了,我们需要产考源码构建方式改造一番。
1|3修复
Activiti7多数据源配置
配置还是要上面的。
- 创建ActivitiConfig继承AbstractProcessEngineAutoConfiguration方法注入SpringProcessEngineConfigurationbean。
- 用@ConfigurationProperties加载以spring.datasource.activiti开头的DataSource
@Configuration publicclassActivitiConfigextendsAbstractProcessEngineAutoConfiguration{ @Bean @ConfigurationProperties(prefix="spring.datasource.activiti") publicDataSourceactivitiDataSource(){ returnDataSourceBuilder.create().build(); } ...略 } SpringProcessEngineConfiguration注入方式改为下面: @Bean @Primary publicSpringProcessEngineConfigurationspringProcessEngineConfiguration( PlatformTransactionManagertransactionManager, SpringAsyncExecutorspringAsyncExecutor, ActivitiPropertiesactivitiProperties, ProcessDefinitionResourceFinderprocessDefinitionResourceFinder, @Autowired(required=false)DefaultActivityBehaviorFactoryMappingConfigurerprocessEngineConfigurationConfigurer, @Autowired(required=false)ListprocessEngineConfigurators, UserGroupManageruserGroupManager, DataSourcedataSource)throwsIOException{ SpringProcessEngineConfigurationconf=newSpringProcessEngineConfiguration(); 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()){ ValidatorSetspringBootStarterValidatorSet=newValidatorSet("activiti-spring-boot-starter"); springBootStarterValidatorSet.addValidator(newAsyncPropertyValidator()); if(conf.getProcessValidator()==null){ ProcessValidatorImplprocessValidator=newProcessValidatorImpl(); 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( newHashSet<>(activitiProperties.getCustomMybatisXMLMappers())); } if(activitiProperties.getCustomMybatisXMLMappers()!=null){ conf.setCustomMybatisXMLMappers( newHashSet<>(activitiProperties.getCustomMybatisXMLMappers())); } if(activitiProperties.isUseStrongUuids()){ conf.setIdGenerator(newStrongUuidGenerator()); } if(activitiProperties.getDeploymentMode()!=null){ conf.setDeploymentMode(activitiProperties.getDeploymentMode()); } conf.setActivityBehaviorFactory(newDefaultActivityBehaviorFactory()); if(processEngineConfigurationConfigurer!=null){ processEngineConfigurationConfigurer.configure(conf); } returnconf; } privatevoidconfigureProcessDefinitionResources( ProcessDefinitionResourceFinderprocessDefinitionResourceFinder, SpringProcessEngineConfigurationconf)throwsIOException{ List procDefResources=processDefinitionResourceFinder .discoverProcessDefinitionResources(); if(!procDefResources.isEmpty()){ conf.setDeploymentResources(procDefResources.toArray(newResource[0])); } }
1|4常见错误
在以上配置中可能会有jdbcUrlisrequiredwithdriverClassName错误
解决办法如下:
#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解决jdbcUrlisrequiredwithdriverClassName错误 #官方文档的解释是: #因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在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
1|5总结
Activiti7国内的教程不是很多,需要自己在社区里或者官方文档,源码去看了解,细心学习。
示例代码地址:activiti-starter
总结
以上所述是小编给大家介绍的解决Springboot2.1.x配置Activiti7单独数据源问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!