报错Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource ...

记录排查springboot项目本地运行正常,部署至服务器无法启动

过程报错Failed to configure a DataSource: 'url’ attribute is not specified and no embedded datasource could be configured.Reason: Failed to deternine a suitable driver class

问题:写了一个用于数据库数据同步的项目,在本地部署测试均正常,服务器也正常,因源代码逻辑不严谨更新了部分代码,重新部署时发现,本地一切正常的项目,但部署至服务器项目无法启动,且报错信息一闪而过,黑屏终端自动关闭,通过截屏方式获取报错信息如下:

报错:

Failed to configure a DataSource: 'url’ attribute is not specified and no embedded

datasource could be configured.
Reason: Failed to deternine a suitable driver class

Consider the following:

If you want an embedded database (H2,HSQL or Derby), please put it on t

he 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:

参考:报错[已解决]: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 -优快云博客

看博主解决思路突然想到自己在更新版本时也另外导入了其他依赖,找到后删除,问题解决,项目运行正常

删除以下依赖

mybatis-spring-boot-starter 原理分析;

参考:mybatis-spring-boot-starter 原理分析-优快云博客

引入mybatis-spring-boot-starter模块之后,其可以:

● 自动检测DataSource

● 使用SqlSessionFactoryBean注册SqlSessionFactory 实例,并设置DataSource数据源

● 基于SqlSessionFactory自动注册SqlSessionTemplate实例

● 自动扫描@Mapper注解类,并通过SqlSessionTemplate注册到Spring Context中

方法2:

看报错信息是数据库连接问题,在我的代码里没有在.yml中配置数据库信息和注解结合使用的方法,而是在application.properties文件中配置,然后在impl文件中进行引用,所以无需在项目启动时就连接数据库,

因此在DataSynchronizationApplication.java启动文件中,可以将@SpringBootApplication修改为@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})就可以启动的时候不需要连接数据库。

到此问题解决,仅记录自己学习过程中的问题与解决方式,如有不妥,请各位大神指导。

### 解决 Spring Boot 数据源配置错误 当遇到 `Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured` 错误时,这表明 Spring Boot 应用程序未能成功配置数据源。以下是几种常见原因及其解决方案: #### 1. 缺少嵌入式数据库依赖项 如果希望使用嵌入式数据库(如 H2、HSQL 或 Derby),则需确保这些库已添加到项目的类路径中。 对于 Maven 项目,在 `pom.xml` 文件中加入相应的依赖项: ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` #### 2. 数据库设置未加载特定环境文件中的属性 如果有针对不同运行模式的数据源参数定义,则应激活相应配置文件来读取它们。默认情况下没有任何活动的profile被启用。 可以在 application.properties 中指定激活某个 profile 的方式如下所示: ```properties spring.profiles.active=dev ``` 同时也要确认对应环境下有完整的 jdbc URL 和其他必要的连接信息存在[^2]。 #### 3. 排除自动装配组件以绕过数据源初始化过程 如果不打算立即建立与外部DBMS之间的联系,可以通过排除某些自动配置类的方法暂时规避此问题的发生。不过这种方法仅适用于确实不需要即时访问持久化层的情况;一旦涉及到实际业务逻辑操作就需要重新审视并修复根本性的缺陷了。 例如通过修改启动类上的注解实现这一点: ```java @SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 注意该做法对 spring-boot 版本有一定要求,并且可能影响其他基于 JPA/MyBatis 等框架的功能正常使用,请谨慎评估后再做决定[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值