【spring boot】 禁用 / 关闭数据源 / DataSource
前言
- spring boot 2.0.0.RELEASE
- maven 3.5
- eclipse 4.9.0
- 用 spring boot 做程序,不需要连接数据库。该程序一直工作正常。
- 在某次修改程序后,出现如下提示:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-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).
显示禁用 DataSource
禁止 spring boot 自动配置数据源。
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
例
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
隐式禁用 DataSource
spring boot 启动时, “DataSourceAutoConfiguration 类” 发现 DataSource 类可用时,才会自动配置 DataSource。
因此,只要保证 spring boot 启动时 “DataSourceAutoConfiguration 类” 找不到 DataSource 类,DataSourceAutoConfiguration 类就会加载失败,也就不会配置 DataSource,进而达到禁用 DataSource 的目的。
隐式禁用 DataSource 失效
承接前言,经过检查 pom.xml,未发现可能引入 DataSource 类的 dependency。
经过检查,发现 classpath 中包含 spring boot start jdbc。
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter-jdbc/2.0.0.RELEASE/spring-boot-starter-jdbc-2.0.0.RELEASE.jar" />
删除 classpath 中与 jdbc、jpa 相关的 jar 即可。
隐世的方式不好控制,图省事儿,可以使用显示方式。
摘抄自网络,便于检索查找。
分类:
old
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律