You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除SpringBoot数据库依赖后启动报错的解决求助

问题

我通过Initializr创建了一个SpringBoot 3.0.6版本的新项目,最初引入了部分数据库相关依赖。在完成基础功能开发后,发现应用无需直接与数据库交互,因此希望移除这些依赖。

我已从build.gradle文件中移除了以下明显的数据库相关依赖:

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.integration:spring-integration-jpa'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'

剩余依赖如下:

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.integration:spring-integration-amqp'
implementation 'org.springframework.integration:spring-integration-http'
implementation 'org.springframework.integration:spring-integration-file'
implementation 'org.springframework.integration:spring-integration-mail'
implementation 'org.springframework.integration:spring-integration-security'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.apache.commons:commons-lang3:3.12.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.springframework.integration:spring-integration-test'
testImplementation 'org.springframework.security:spring-security-test'

同时我也从application.properties中删除了数据库连接信息:

server.servlet.context-path=/MyApp

instance.lock=false

#debug=false
spring.devtools.restart.log-condition-evaluation-delta=false

logging.level.org.me=debug
logging.level.org.springframework=info
logging.level.root=warn

server.ssl.enabled=false
server.port=8111
server.ssl.key-store=src/main/resources/server.jks
server.ssl.key-store-password=JohnDoe
server.ssl.key-password=blahblah

# TEST:
spring.rabbitmq.host=rmq-server
spring.rabbitmq.port=5671
spring.rabbitmq.username=messageUser
spring.rabbitmq.password=abc123

spring.rabbitmq.virtualhost=/vhost
spring.rabbitmq.ssl.enabled=true
spring.rabbitmq.my.request.queue=me.q.app.request
spring.rabbitmq.my.response.queue=me.q.app.response

现在启动应用时出现以下报错:

2023-05-18T14:16:32.376-05:00  WARN 29920 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2023-05-18T14:16:32.429-05:00  INFO 29920 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-05-18T14:16:32.453-05:00 ERROR 29920 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be 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).


Process finished with exit code 0

显然仍有配置或依赖在告知Spring需要数据库,但我无法定位问题所在。恳请提供建议,说明还需移除哪些内容以彻底清除数据库相关依赖。

我已尝试执行gradle clean build --refresh-dependencies、在IntelliJ中重建项目,还创建了一个无数据库依赖的同类SpringBoot项目进行对比,但未发现相关差异。运行gradle dependencies也未返回任何包含jdbc、jpa或sql的依赖。

解决方案

核心问题是Spring Boot自动配置仍在尝试初始化数据源,即使已移除显式依赖,可按以下步骤彻底解决:

  • 禁用数据库相关自动配置
    在Spring Boot启动类上添加注解,直接排除所有数据库相关的自动配置类:
@SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class,
    SqlInitializationAutoConfiguration.class
})
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}
  • 清理项目中的数据库相关代码

    • 搜索并删除/注释带有@Entity、@Repository、JpaRepository这类注解的类,这类注解会触发JPA自动配置。
    • 检查自定义配置类,删除手动创建的DataSource、JdbcTemplate等数据库相关Bean。
  • 清理测试代码中的数据库依赖
    如果测试不需要数据库,在测试类上同样添加@SpringBootTest(exclude = ...)排除相关配置,或直接删除测试中涉及数据库的逻辑。

  • 再次验证依赖
    用更精确的命令排查是否有间接引入的数据库依赖:

./gradlew dependencies | grep -i jdbc
./gradlew dependencies | grep -i jpa
./gradlew dependencies | grep -i sql

若发现间接依赖,找到对应的父依赖并添加排除规则。

内容的提问来源于stack exchange,提问作者WishIWasJeeping

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 19:54:54