SpringBoot 项目:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded

文章目录


前言

今天启动项目时突然报错数据库未指定url。
在这里插入图片描述
我直接满脸问号,查了一下git的提交记录,发现代码并没有动过。很奇怪,昨天还能启动的代码,第二天启动就报错了。

spring:
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
     url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
     username: root
     password: 123456
     driver-class-name: com.mysql.cj.jdbc.Driver

仔细检查了一下配置文件,发现没有问题。

完整报错信息

2024-09-29 10:16:52.201  INFO 3316 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-09-29 10:16:52.201  INFO 3316 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2024-09-29 10:16:52.315  INFO 3316 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-09-29 10:16:52.316  INFO 3316 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1260 ms
2024-09-29 10:16:52.472  INFO 3316 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2024-09-29 10:16:52.511  WARN 3316 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceRecordServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceRecordMapper' defined in file [D:\qbh\code\test\project_test\target\classes\com\qf\mapper\DeviceRecordMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is 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
2024-09-29 10:16:52.513  INFO 3316 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2024-09-29 10:16:52.528  INFO 3316 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-09-29 10:16:52.533 ERROR 3316 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

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. 在 @SpringBootApplication 注解上加上 exclude ,解除自动加载DataSourceAutoConfiguration。
  • 由于SpringBoot 自动配置的原因,SpringBoot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。

  • 当工程中没有关于dataSource相关的配置信息(或者没有检查到dataSource),当Spring创建dataSource bean因缺少相关的信息就会报错。

  • 因此在类定义上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})的注解,不让SpringBoot自动配置DataSourceAutoConfiguration

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

在重新加载maven
在这里插入图片描述
项目就可以启动了。

  1. 重新加载maven
    如果跟我一样的情况,之前项目可以正常运行,但是突然报错了,是spring在启动时没有检查到dataSource。因此可以直接重新加载maven,项目也就可以正常运行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值