Sub topic 8
Seeing what was applied: the conditions report
This chapter assumes you can run a Spring Boot application and know what a bean and an application context are. It also assumes you have read how @ConditionalOnClass and @ConditionalOnMissingBean decide whether an auto-configuration applies. Here you will learn to see those decisions instead of guessing at them.
Why you need the report
Auto-configuration runs before your application code and leaves no visible trace. When a bean is missing, or a bean you did not write is present, the cause is usually a condition that evaluated differently than you expected. Boot records every condition it evaluates. That record is the conditions report.
Turning the report on
Start the application with the --debug flag. Setting debug=true in application.properties has the same effect.
java -jar app.jar --debugBoot prints the report to the console during startup. Setting debug also raises the log level for some core loggers, so the output is noisier than usual. It does not turn on debug logging for your own code.
Reading the report
The report is called the conditions evaluation report. It has four sections.
| Section | What it lists |
|---|---|
| Positive matches | Auto-configurations whose conditions all passed, so they were applied |
| Negative matches | Auto-configurations that were skipped, with the condition that failed |
| Exclusions | Classes you removed with `exclude` or `spring.autoconfigure.exclude` |
| Unconditional classes | Auto-configurations that have no conditions |
A trimmed example looks like this:
Positive matches:
-----------------
GreetingAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.example.greeting.GreetingService' (OnClassCondition)
GreetingAutoConfiguration#greetingService matched:
- @ConditionalOnMissingBean (types: com.example.greeting.GreetingService; SearchStrategy: all) did not find any beans (OnBeanCondition)
Negative matches:
-----------------
DataSourceAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.sql.DataSource' (OnClassCondition)Each entry names the class or the @Bean method, then gives the reason in plain words. The name in parentheses is the condition class that made the decision. Entries for a @Bean method are written as ClassName#methodName. This lets you tell whether the whole configuration was skipped or only one bean inside it.
A diagnosis workflow
- Search the output for the name of the auto-configuration you expect.
- If it is under negative matches, read the failed condition. A missing class means a dependency is absent. A failed
@ConditionalOnPropertymeans a property has the wrong value or is not set. - If it is under positive matches but your bean behaves differently, look for a second bean of the same type. A
@ConditionalOnMissingBeanentry with a "found bean" message tells you your own bean won. - If it is not listed at all, the class was never registered as a candidate, or it was excluded. Check the exclusions section and the
AutoConfiguration.importsfile.
Exclusions and typos
If you exclude a class, it appears under Exclusions. Boot fails at startup when an excluded class is not on the classpath, which catches many typos. Using the exclude attribute of @SpringBootApplication gives you a compile-time check on the class name. The property form does not.
# application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationOther ways to inspect
With Spring Boot Actuator on the classpath, the conditions endpoint returns the same data as JSON. Expose it with management.endpoints.web.exposure.include=conditions. This helps when the application is already running and you cannot restart it with a flag.
Exercise
Your application has spring-boot-starter-web but no DataSource bean is created, and you expected one. In which section of the conditions report do you look first, and what kind of reason do you expect to find?
Show answer
Look under negative matches for DataSourceAutoConfiguration. The likely reason is a failed @ConditionalOnClass, because no JDBC starter or database driver is on the classpath, so javax.sql.DataSource support or an embedded database class is missing. Adding spring-boot-starter-jdbc and a driver fixes it.
Exercise
A bean from an auto-configuration appears under positive matches for its class, but your own bean of the same type is the one injected. Which condition explains this?
Show answer
@ConditionalOnMissingBean on the auto-configured @Bean method. The report lists that method as a negative match because Boot found your bean, so the auto-configured one backed off.