Sub topic 3
Seeing What Was Applied: The Conditions Report
Assume you know Spring beans, dependency injection, Java configuration, and basic Maven or Gradle dependency management.
Reading the Conditions Report
Spring Boot evaluates auto-configuration conditions at startup. Run the application with --debug:
java -jar app.jar --debugThe report lists:
- Positive matches: configurations whose conditions passed.
- Negative matches: configurations whose conditions failed.
- Exclusions: configurations deliberately excluded by the application.
For example, a database auto-configuration may fail because no JDBC driver is present. A configuration may also back off because your application already defines the required bean.
Discovery and Ordering
Spring Boot discovers auto-configurations from:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsEach line names one auto-configuration class:
com.example.greeting.GreetingAutoConfigurationBoot then orders these classes according to their relationships. Use @AutoConfiguration(before = ...) or @AutoConfiguration(after = ...) when one configuration must run before or after another.
Writing and Testing Auto-Configuration
A custom configuration should use conditions and externalized properties:
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}@ConditionalOnClass requires a class on the classpath. @ConditionalOnMissingBean lets an application-defined bean replace the default. Add @ConditionalOnProperty when a property should enable or disable the feature.
Test the decisions without starting a server:
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GreetingAutoConfiguration.class))
.run(context -> assertThat(context).hasSingleBean(GreetingService.class));Add .withBean(...) in another test to verify that the auto-configuration backs off.
Packaging a Custom Starter
A starter is a dependency bundle. It normally contains the auto-configuration module as a transitive dependency. Name application-facing starters with a project prefix, such as acme-greeting-spring-boot-starter. Keep implementation details in a separate module, such as acme-greeting-spring-boot-autoconfigure.
Exercise
What does a negative match in the conditions report mean?
Show answer
An auto-configuration was considered, but at least one of its conditions did not pass.