Sub topic 12
Excluding auto-configurations
This chapter assumes you can read the --debug conditions report and know how @ConditionalOnMissingBean makes an auto-configuration back off. You should also be comfortable with application.properties.
Why exclude
Sometimes an auto-configuration matches but you don't want it. A common case is DataSourceAutoConfiguration when a library puts a JDBC driver on the classpath and your application never uses a database. Without a spring.datasource.url, startup fails.
Exclusion removes the class from the candidate list before any of its conditions are evaluated. The class contributes no beans and none of its conditions are checked.
Excluding with the annotation
@SpringBootApplication exposes the exclude attribute of @EnableAutoConfiguration.
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
}This form is checked by the compiler. A renamed or removed class breaks the build instead of failing silently later.
If the class may not be on the classpath, or you don't want a compile-time dependency on it, use excludeName with the fully qualified name:
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(excludeName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")
public class Application {
}Excluding with a property
The spring.autoconfigure.exclude property takes a comma-separated list of class names. It needs no code change, so you can set it per profile or per environment:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationThe property and the annotation attributes are merged. A class excluded in either place is excluded.
Choosing an approach
Exclusion is the bluntest tool. Prefer a narrower one when it exists:
- A feature flag such as
greeting.enabled=falseleaves the rest of the class intact. - A user-defined bean makes
@ConditionalOnMissingBeanback off for that one bean only. - Exclusion drops everything the class would have registered, including beans that other auto-configurations may depend on.
Common errors
Excluding a class that is not an auto-configuration fails at startup. Spring Boot reports that the listed classes "could not be excluded because they are not auto-configuration classes". The usual causes are a typo or a class that is an ordinary @Configuration. Component scanning picks up ordinary configuration classes regardless of exclusion, so use @ComponentScan filters for those.
A second mistake is excluding a class and then being surprised by the missing beans. Anything that injects those beans now fails with NoSuchBeanDefinitionException. Exclude only when you will supply replacements or nothing consumes them.
Verifying the exclusion
Run the application with --debug and read the report:
java -jar app.jar --debugThe report has an Exclusions section listing every excluded class. An excluded class does not appear under positive or negative matches, because its conditions were never evaluated. If your class shows up under negative matches instead, the exclusion did not take effect. Check the spelling of the name and where the property is defined.
Exercise
Your application starts with a library whose auto-configuration fails when no spring.datasource.url is set, and you never use a database. Name two ways to exclude DataSourceAutoConfiguration, and say how you would confirm the exclusion worked.
Show answer
Use @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) (or excludeName with the fully qualified name), or set spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration in application.properties. To confirm, start with --debug and check that the class is listed under Exclusions in the conditions evaluation report and not under positive or negative matches.