【已解决】SpringBoot 启动报错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no emb

太过爱你忘了你带给我的痛 2024-02-19 09:08 170阅读 0赞

下面是解决方法,快速搞定。

场景

初始化创建的 SpringBoot ,什么都没改怎么就报错了?

文章目录

    • 场景
    • 错误信息
    • 解决方案
      • 方案1
      • 方案2
      • 方案三:
    • 原因

错误信息

  1. 2023-11-06 22:09:42.132 INFO 2364 --- [ main] ConditionEvaluationReportLoggingListener :
  2. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  3. 2023-11-06 22:09:42.144 ERROR 2364 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
  4. ***************************
  5. APPLICATION FAILED TO START
  6. ***************************
  7. Description:
  8. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  9. Reason: Failed to determine a suitable driver class
  10. Action:
  11. Consider the following:
  12. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  13. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
  14. Process finished with exit code 1

在这里插入图片描述

解决方案

方案1

如果项目不需要数据库相关信息就排除此类的autoconfig
在 @SpringBootApplication 注解上加上 exclude ,解除自动加载DataSourceAutoConfiguration。

  1. @SpringBootApplication(exclude= {
  2. DataSourceAutoConfiguration.class})

springboot启动类加上这个启动以后就可以正常运行。完整代码:

  1. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application, args);
  5. }
  6. }

在这里插入图片描述

方案2

配置文件添加数据库链接信息

.properties 文件添加数据库配置信息(以mysql为例):

  1. spring.datasource.name=test
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

.yml 文件添加数据库配置信息(已mysql为例):

  1. spring:
  2. datasource:
  3. name: test
  4. url: jdbc:mysql://localhost:3306/test
  5. username: root
  6. password: 123456
  7. driver-class-name: com.mysql.jdbc.Driver

方案三:

配置pom.xml中yml或者properties扫描

需要在 pom 文件中 中添加如下来保证文件都能正常被扫描到并且加载成功。

  1. <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.yml</include>
  7. <include>**/*.properties</include>
  8. <include>**/*.xml</include>
  9. </includes>
  10. <filtering>false</filtering>
  11. </resource>
  12. <resource>
  13. <directory>src/main/resources</directory>
  14. <includes>
  15. <include>**/*.yml</include>
  16. <include>**/*.properties</include>
  17. <include>**/*.xml</include>
  18. </includes>
  19. <filtering>false</filtering>
  20. </resource>
  21. </resources>

原因

根据报错日志分析是在 SpringBoot 项目启动的时候没有找到 database 数据库连接地址,我们知道在 SpringBoot 启动的时候会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 这个类,而 DataSourceAutoConfiguration 类使用了 @Configuration 注解向spring注入了dataSource bean,又因为项目中并没有关于 dataSource 相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。

发表评论

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

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

相关阅读