The 30 second answer
Excluding an auto-configuration removes it from Spring Boot's candidate list before condition filtering, so it won't contribute its configuration or beans through that mechanism. I can use the exclude or excludeName attributes on @SpringBootApplication, or the spring.autoconfigure.exclude property. A class that's present but isn't a valid auto-configuration causes an error. Exclusion doesn't remove the library itself, and I may need to supply beans that other components still require.
The exclusions are collected from three places:
protected Set<String> getExclusions(AnnotationMetadata metadata, @Nullable AnnotationAttributes attributes) {
Set<String> excluded = new LinkedHashSet<>();
if (attributes != null) {
excluded.addAll(asList(attributes, "exclude"));
excluded.addAll(asList(attributes, "excludeName"));
}
excluded.addAll(getExcludeAutoConfigurationsProperty());
return getAutoConfigurationReplacements().replaceAll(excluded);
}AutoConfigurationImportSelector.java L235-L243. The property name is spring.autoconfigure.exclude (L77).
Then, in getAutoConfigurationEntry, checkExcludedClasses validates the list and configurations.removeAll(exclusions) removes them before the condition filter runs (L137-L140).
Three ways to exclude:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)@SpringBootApplication(excludeName = "org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration")spring:
autoconfigure:
exclude: org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfigurationThe property form is useful per profile or per environment, for example to turn off a feature in tests.
Consequences:
- Any bean that depended on the excluded beans now fails with
NoSuchBeanDefinitionException, unless you provide those beans yourself. - Other auto-configurations that were ordered "after" the excluded one still run, and their
@ConditionalOnBeanchecks may now fail. - The excluded class appears in the Exclusions section of the
--debugcondition evaluation report.
Exclude vs. back-off: often you do not need to exclude anything. Defining your own bean makes @ConditionalOnMissingBean skip the default. Exclusion is for when you want the whole configuration gone, such as starting an app without a database even though the JDBC driver is present.
Spring Boot 4 note: many auto-configurations moved packages (for example, DataSourceAutoConfiguration is now in org.springframework.boot.jdbc.autoconfigure). Check names when upgrading; the replacements step above maps some old names.