Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could

今天写springboot3项目时,启动项目报错:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-01-30T15:51:34.876+08:00 ERROR 2904 --- [           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).


Process finished with exit code 1

大概意思是没有配置数据源,检查了yml文件,发现url,用户名,密码都没啥问题,于是去看看是是不是依赖的问题,然后果然在整合druid依赖时出现问题:

上述依赖是springboot2整合时用的依赖,因为是第一次使用springboot3写项目,整合依赖时没注意,导入下面依赖就可以了:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-3-starter</artifactId>
    <version>1.2.20</version>
</dependency>

再次启动项目:

就没有什么问题了.。。。。。

但是如果使用druid-spring-boot-starter依赖时:

可以在启动类手动配置和创建Druid数据源如下:

  1. @ConfigurationProperties(prefix = "spring.datasource.druid"): 这是一个Spring注解,它表示该类(通常是配置类)将绑定以“spring.datasource.druid”为前缀的属性。这意味着你可以在Spring的配置文件中(如application.properties或application.yml)设置以“spring.datasource.druid.”为前缀的属性,并且这些属性将自动绑定到这个配置类的字段上。
  2. @Bean: 这是另一个Spring注解,表示该方法将返回一个对象,该对象应被注册为Spring应用上下文中的一个bean。这样其他组件或bean就可以注入或使用这个bean。
  3. public DataSource druidDataSource() { return new DruidDataSource(); }: 这是一个公共方法,返回一个DataSource类型的对象。这个方法被标记为@Bean,所以Spring会将其注册为一个bean。在这个方法中,创建了一个新的DruidDataSource对象并返回它。

总体来说,这段代码定义了一个Spring bean,该bean是Druid数据源。通过使用@ConfigurationProperties注解,你可以轻松地通过配置文件来配置这个数据源

启动项目也是可以的

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值