【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 即可。
隐世的方式不好控制,图省事儿,可以使用显示方式。

posted @ 2024-06-27 10:50  CharyGao  阅读(39)  评论(0编辑  收藏  举报