首先看错误信息

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

如上所示,DruidDataSourceAutoConfigure Failed to determine a suitable driver class,即druid找不到mysql driver,然而mysql的驱动包啥的都没问题,于是直接点进DruidDataSourceAutoConfigure查看源码

如上,标红表明druid是根据spring.datasource.druid找jdbc属性的,如果not found,则根据spring.datasource找jdbc属性,一般而言这是不会出现错误的。但是我这里使用了shardingjdbc,配置如下:

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds0
        username: xxx
        password: xxx
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds1
        username: xxx
        password: xxx
    sharding:
      tables:
        user:
          actual-data-nodes: ds${0..1}.user${0..1}
          database-strategy:
            inline:
              sharding-column: id
              algorithm-expression: ds${id % 2}
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: user$->{id % 2}
          key-generator:
            type: SNOWFLAKE
            column: order_id
    props:
      sql:
        show: true
      executor:
        size: 12

就很显然了,他根据spring.datasource.druid或者spring.datasource确实找不到,因为我的结构是spring.shardingsphere.datasource。

解决方式1:

如果我们用的jar包是druid-spring-boot-starter,则在启动类上排除druid自动配置

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})

解决方式2:

不用

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.22</version>
</dependency>

改为

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.22</version>
</dependency>

即可

 

GitHub 加速计划 / druid / druid
6
4
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:4 个月前 )
933dee04 - 11 天前
3246166f - Add constructor with SQLParserFeature support to SparkStatementParser - Fix SQLParserUtils to pass features parameter to SparkStatementParser, CKStatementParser and StarRocksStatementParser 21 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐