如何解决Springboot2.1.x配置Activiti7单独数据源问题

本篇内容介绍了“如何解决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.Driverspring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=truespring.datasource.activiti.username=rootspring.datasource.activiti.password=Rtqw123OpnmERspring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver

用@ConfigurationProperties加载以spring.datasource.activiti开头的DataSource。  创建ActivitiConfig继承AbstractProcessEngineAutoConfiguration方法注入SpringProcessEngineConfigurationbean,调用AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration方法把创建的数据源注入。

@Configurationpublic 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方法已经不存在了,我们需要产考源码构建方式改造一番。

1|3修复

Activiti7多数据源配置

配置还是要上面的。

创建ActivitiConfig继承AbstractProcessEngineAutoConfiguration方法注入SpringProcessEngineConfigurationbean。  用@ConfigurationProperties加载以spring.datasource.activiti开头的DataSource

@Configurationpublic 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 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 procDefResources = processDefinitionResourceFinder    .discoverProcessDefinitionResources();  if (!procDefResources.isEmpty()) {   conf.setDeploymentResources(procDefResources.toArray(new Resource[0]));  } }

1|4常见错误

在以上配置中可能会有jdbcUrl is required with driverClassName错误

解决办法如下:

# activiti 数据源spring.datasource.activiti.driver=com.mysql.jdbc.Driverspring.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=rootspring.datasource.activiti.password=Rtqw123OpnmERspring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver

“如何解决Springboot2.1.x配置Activiti7单独数据源问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


文章题目:如何解决Springboot2.1.x配置Activiti7单独数据源问题
当前URL:http://azwzsj.com/article/ihcisg.html