SpringBoot启动报错Failed to configure a DataSource: ‘url‘ attribute is not specified

本文介绍了在项目引用数据源starter但配置不一致时,如何通过排除自动配置类解决启动错误。重点讲解了排除DataSourceAutoConfiguration和DruidDataSourceAutoConfigure的情况,并提到了根据项目需求选择原生配置或排除配置类的重要性。

错误:

启动错误信息:

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 (no profiles are currently active).


1、项目不需要使用 DataSource,但是引用了JPA的数据包,这里需要在启动类上增加:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }

排除数据源的自动配置类。

2、项目使用了Druid, 这时而又非原生的配置,比如增加了动态数据源支持。这时需要在启动类上增加:

@SpringBootApplication(exclude ={DruidDataSourceAutoConfigure.class} )

排除掉Druid的数据源的自动配置类。

总结:项目引用了数据源配置的starter,但是配置文件又未按照starter里的配置方式配置,这时就会出现上面的问题。解决方式就是要么按照原生配置方式来配置,要么排除掉这个自动配置类。

 

 

当 Java 启动出现 “Failed to configure a DataSource: 'url' attribute is not specified and no embedded” 报错,通常是因为数据源配置中缺少 `url` 属性,且没有合适的嵌入式数据源配置。以下是一些常见的解决办法: ### 检查配置文件 要保证在配置文件(像 `application.properties` 或者 `application.yml`)里正确配置了数据库的 `url`、`username` 和 `password`。 #### application.properties 示例 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` #### application.yml 示例 ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database_name username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.Driver ``` ### 检查数据库驱动依赖 要确保项目里添加了正确的数据库驱动依赖。以使用 MySQL 数据库为例,在 `pom.xml` (Maven 项目)中添加以下依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` ### 激活特定配置文件 要是数据库配置存储在特定的配置文件中,比如 `application-dev.properties` 或者 `application-prod.yml`,就需要激活对应的配置文件。可以通过以下两种方式激活: #### 通过命令行参数 在启动应用时,添加 `-Dspring.profiles.active=dev` 参数。 #### 在配置文件中设置 在 `application.properties` 或者 `application.yml` 中添加: ```properties spring.profiles.active=dev ``` ```yaml spring: profiles: active: dev ``` ### 检查嵌入式数据库 若想使用嵌入式数据库(如 H2、HSQL 或 Derby),要确保将其添加到类路径中。以 H2 数据库为例,在 `pom.xml` 中添加以下依赖: ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` ### 检查配置文件路径 要保证配置文件位于正确的路径下,一般是 `src/main/resources` 目录。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值