☰
Spring Boot 排除自动配置
2026/9/28 6:01:27 网站建设 项目流程
Spring Boot 排除自动配置

Spring Boot 自动配置非常强大,有时需要排除/禁用 Spring Boot 某些类的自动化配置。

*************************** 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).
方式1

@SpringBootApplication注解使用时,使用exclude属性进行排除指定的类:

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

当自动配置类不在类路径下时,使用excludeName属性进行排除指定的类名全路径:

@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"}) public class Application { // ... }
方式2

配置文件中指定参数spring.autoconfigure.exclude进行排除:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

或

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

或

spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration - org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询