SpringBoot 引入dynamic和druid找不到数据源问题

当在SpringBoot项目中引入多数据源和Druid依赖后,启动时出现找不到数据源的错误。原因是SpringBoot自动配置导致。解决办法是在启动类上排除Druid的自动配置类,如`@SpringBootApplication(exclude=DruidDataSourceAutoConfigure.class)`。
摘要由CSDN通过智能技术生成

分享知识 传递快乐

因为项目需要的原因在 pom.xml 中引入:

<!-- 多数据源 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>

<!-- druid 官方 starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

但在项目启动时提示找不数据源,出现以下错误提示:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (the profiles test are currently active).

数据源明明已经正确配置了,怎么会出现找不到数据源?

原因

SpringBoot 启动时自动配置数据库信息导致。

因为 DruidDataSourceAutoConfigure 在 DynamicDataSourceAutoConfiguration 之前会注入一个 DataSourceWrapper,会在原生的 spring.datasource 下找 url,username,password 信息。

在启动类上排除掉自动配置类即可,如下:

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)


 

—————————
如有不足请留言指正
相互学习,共同进步

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Spring Boot可以通过引入相应的依赖来实现对MySQL和Druid的支持。 1. 引入MySQL依赖 在pom.xml文件中添加如下依赖: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 配置MySQL连接信息 在application.properties或application.yml文件中添加如下配置: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 其中,url为MySQL的连接地址,username和password为MySQL的连接用户名和密码,driver-class-name为MySQL的JDBC驱动类名。 3. 引入Druid依赖 在pom.xml文件中添加如下依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> ``` 4. 配置Druid连接池信息 在application.properties或application.yml文件中添加如下配置: ``` spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.initial-size=5 spring.datasource.min-idle=5 spring.datasource.max-active=20 spring.datasource.max-wait=60000 spring.datasource.time-between-eviction-runs-millis=60000 spring.datasource.min-evictable-idle-time-millis=300000 spring.datasource.validation-query=SELECT 1 FROM DUAL spring.datasource.test-while-idle=true spring.datasource.test-on-borrow=false spring.datasource.test-on-return=false spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ``` 其中,type为Druid数据源的类型,initial-size为连接池初始化大小,min-idle为最小空闲连接数,max-active为最大活跃连接数,max-wait为最长等待时间,time-between-eviction-runs-millis为检测连接是否有效的时间间隔,min-evictable-idle-time-millis为最小空闲时间,validation-query为检测连接是否有效的SQL语句,test-while-idle为是否检测空闲连接,test-on-borrow为是否检测连接可用性,test-on-return为是否检测连接归还情况,filters为Druid连接池的过滤器,connectionProperties为连接属性。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旷野历程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值