SpringBoot Failed to configure a DataSource: ‘url‘ attribute is not specified

今天药忘吃喽~ 2023-10-08 15:02 20阅读 0赞

问题描述

  1. Description:
  2. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  3. Reason: Failed to determine a suitable driver class
  4. Action:
  5. Consider the following:
  6. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  7. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

问题原因

在pom.xml中引入了持久层的依赖,同时配置文件中关于持久层的配置错误或未配置导致。

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>${spring-boot.mybatis}</version>
  5. </dependency>

当项目中加入以上依赖后,SpringBoot会默认加载DataSourceAutoConfiguration对象,而该对象使用了Configuration注解向Spring容器中注入DataSource相关的Bean,而项目中缺少正确的DataSource的配置信息,导致Spring容器创建DataSource相关的Bean失败,进而启动报错。

解决方案

①. 如果需要持久层,则需要配置并检查持久层的配置信息保证其正确。
②. 如果不需要持久层,则可以屏蔽掉Spring容器自动装配数据源的功能。

  1. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  2. public class MyApplication {
  3. ...
  4. }

发表评论

表情:
评论列表 (有 0 条评论,20人围观)

还没有评论,来说两句吧...

相关阅读