前言

  • 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