Spring 通过配置引导 2 数据源不起作用

Spring Boot 2 datasource by configuration does not work

我有一个 application.yml(!) 的配置包含以下内容:

spring:
  datasource:
    url: "jdbc:h2:file:./camunda-h2-database"
    driverClassName: "org.h2.Driver"
    username: "sa"
    password: ""

这给了我错误消息:

2021-03-11T17:02:48,600 WARN  [main] o.s.c.s.AbstractApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.camunda.bpm.spring.boot.starter.CamundaBpmAutoConfiguration$ProcessEngineConfigurationImplDependingConfiguration': Unsatisfied dependency expressed through field 'processEngineConfigurationImpl'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'processEngineConfigurationImpl' defined in class path resource [org/camunda/bpm/spring/boot/starter/CamundaBpmConfiguration.class]: Unsatisfied dependency expressed through method 'processEngineConfigurationImpl' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'camundaDatasourceConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这样做可行:

public DriverManagerDataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:file:./camunda-h2-database");
    dataSource.setUsername("sa");
    dataSource.setPassword("");

    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(dataSource());
}

想法? :-)

driverClassName 重命名为 driver-class-name,因为在 Spring 启动自动配置属性中,Kebab 大小写优先于 Camel 大小写。

如果这没有帮助,请确保您没有关闭 DataSourceAutoConfiguration 功能。它通常看起来像

@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])
public class BootApplication { ... }