Sub topic 7
Overriding, Backing Off, and Excluding Auto-Configuration
This chapter assumes you know beans, @Configuration and @Bean, and how to run a simple Spring Boot application. You should also have seen conditional annotations such as @ConditionalOnMissingBean and @ConfigurationProperties.
Auto-configuration gives you defaults. It does not take control away from you. Boot offers three ways to change what it does, and each one suits a different situation.
Three ways to change a default
| Goal | Mechanism | Where |
|---|---|---|
| Tune a value | Set a property | `application.properties` |
| Replace a bean | Define your own bean of that type | Your `@Configuration` class |
| Remove the whole configuration | Exclude it | Annotation or property |
Prefer the first option that works. A property change is the smallest change, and an exclusion is the largest.
Backing off when you define a bean
Most Boot auto-configurations mark their beans with @ConditionalOnMissingBean. Boot checks whether the application context already holds a bean of that type. If it does, the auto-configured bean is skipped. This is called backing off.
Here is the library bean from the Greeting example:
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}To override it, declare your own bean in your application:
@Configuration
public class MyConfig {
@Bean
public GreetingService greetingService() {
return new GreetingService("Hi");
}
}Your bean is registered, so the auto-configured one never is. Note that the greeting.message property no longer has any effect here, because GreetingProperties is only used by the bean that backed off.
Common mistakes when overriding
- Different bean type. The condition matches by type. If your bean is declared with a return type that is not the expected type or a subtype, the default is not skipped and you may end up with two beans.
- Overriding too much. Replacing a large bean such as a
DataSourcemeans you now own all of its setup. Check whether a property would do the job. - Component scanning does not reach it. Your override must be picked up by Spring. Keep it in a package under your
@SpringBootApplicationclass.
Excluding an auto-configuration
Sometimes you want an auto-configuration gone entirely. A common case is a library on the classpath that you do not want Boot to configure, such as a database starter pulled in transitively. You can exclude it in code:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}Or you can exclude it with a property, which needs no code change:
# application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationIf the class might not be on the classpath, use excludeName with the fully qualified class name as a string. This avoids a compile error.
Checking the result
When a change does not behave as expected, run with --debug to print the conditions report. It shows which auto-configurations matched, which backed off, and which were excluded. The next chapter covers how to read it.
Exercise
Your application defines its own GreetingService bean, and you also set greeting.message=Welcome in application.properties. The greeting still says "Hi", the value from your bean. Why does the property have no effect, and how could you make it work?
Show answer
The auto-configured GreetingService backed off because of @ConditionalOnMissingBean, so the only bean that read GreetingProperties was never created. Your own bean ignores the property. To use the property, inject GreetingProperties into your bean method and pass properties.getMessage() to the constructor, or remove your bean and let the default apply.
Exercise
You want to stop Boot from configuring a data source without changing any Java code. Which setting do you use?
Show answer
Set spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration in application.properties.