【spring boot】 禁用/关闭数据源/DataSource

编程知识 更新时间:2023-04-05 04:15:18

前言

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

更多推荐

【spring boot】 禁用/关闭数据源/DataSource

本文发布于:2023-04-05 04:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/2c530858bb84fce3f334e65e877dc100.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   spring   boot   DataSource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44717文章数
  • 14阅读数
  • 0评论数