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

排除DataSourceAutoConfiguration报错:非自动配置类问题求助

解决Spring Boot打包后排除DataSourceAutoConfiguration报错问题

问题场景

本地通过SpringToolSuite4运行以下代码时正常:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MainApplication {
}

但将项目打包为jar包在独立环境运行时,抛出错误:

2023-07-01 08:19:41,379 [main] ERROR SpringApplication -Application run failed
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:
        - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

        at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.handleInvalidExcludes(AutoConfigurationImportSelector.java:222)
        at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.checkExcludedClasses(AutoConfigurationImportSelector.java:208)

解决方案

  • 检查依赖打包范围:确保spring-boot-autoconfigure依赖的范围为compile(Maven默认配置),不要设置为provided或其他会在打包阶段排除的范围,否则运行时无法找到该类,导致Spring无法识别其为自动配置类。
  • 改用配置文件排除:放弃注解中排除的方式,改为在application.properties或application.yml中配置,规避类路径加载问题:
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    若使用YAML格式:
    spring:
      autoconfigure:
        exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
  • 校验Spring Boot版本一致性:确认本地开发环境与打包运行环境使用的Spring Boot版本完全一致,不同版本的自动配置类结构或加载逻辑可能存在差异。
  • 清理打包缓存:执行构建工具的清理命令(如Maven的mvn clean install,Gradle的./gradlew clean build),清除旧编译缓存文件,避免缓存导致的类加载异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 15:03:12