Sub topic 11
Finding out why something did or did not apply
This chapter assumes you can run a Spring Boot application, read application.properties, and know what @Configuration and @Bean do. You should also know the @Conditional family from the earlier chapters. Here you will learn to read the conditions evaluation report, which shows what Spring Boot decided about each auto-configuration and why.
Why you need the report
Auto-configuration is silent when it works. When a bean you expected is missing, or one you did not want is present, guessing is slow. Spring Boot records every condition it evaluates and can print the results.
Turning the report on
Start the application with the --debug flag:
java -jar app.jar --debugYou can also set debug=true in application.properties. This switches on the conditions report. It does not switch on debug logging for your own code.
The report is written to the console at startup under the heading CONDITIONS EVALUATION REPORT. If the application fails to start, Spring Boot prints the same report again, so you can use it to diagnose startup failures.
The sections of the report
The report has up to four sections.
| Section | Meaning |
|---|---|
| Positive matches | Auto-configurations whose conditions all passed, so they were applied. |
| Negative matches | Auto-configurations where a condition failed, so they were skipped. |
| Exclusions | Classes you removed explicitly. |
| Unconditional classes | Auto-configurations with no conditions at all. |
A typical entry looks like this:
Positive matches:
-----------------
GreetingAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.example.greeting.GreetingService' (OnClassCondition)
- @ConditionalOnProperty (greeting.enabled=true) matched (OnPropertyCondition)
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 'io.r2dbc.spi.ConnectionFactory' (OnClassCondition)Each line names the condition and the reason. Entries appear at two levels: the whole class (GreetingAutoConfiguration) and individual bean methods (GreetingAutoConfiguration#greetingService). A class can match while one of its methods does not.
Working through a diagnosis
Follow the same steps each time.
- Start the application with
--debug. - Search the output for the class or bean name you care about.
- Check whether it is under Positive matches or Negative matches.
- Read the reason after "Did not match".
- Fix the cause: add a dependency, set a property, or remove a conflicting bean.
Common findings
- Missing class. A
@ConditionalOnClassfailure means a dependency is not on the classpath. Add the library. - Existing bean. A
@ConditionalOnMissingBeanfailure means a bean of that type already exists, often one you defined. The auto-configuration backed off as designed. - Property value. A
@ConditionalOnPropertyfailure means the property is absent or has a different value. Check spelling and profile-specific files. - Ordering. A
@ConditionalOnBeanfailure for a bean you know exists can mean the class that defines it was processed later. Check thebeforeandafterattributes. - Not listed at all. The class is not in the imports file, or you excluded it.
Checking your own auto-configuration
Suppose GreetingService is not created in your application. Run with --debug and search for GreetingAutoConfiguration. If it is absent from every section, the imports file is not being found. Confirm the file is at META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports and that the jar contains it. If the class is under Negative matches, the message tells you which condition failed.
Exercise
Exercise
Your application starts with --debug. The report shows AuditAutoConfiguration under Negative matches with "@ConditionalOnBean did not find required type 'javax.sql.DataSource'", yet your application has a working data source. What is the most likely cause, and where do you look next?
Show answer
The auto-configuration was probably processed before the class that registers the DataSource, so the bean definition did not exist yet when the condition ran. Check that AuditAutoConfiguration declares after = DataSourceAutoConfiguration.class in @AutoConfiguration. If the DataSource comes from another auto-configuration, add that class to after instead.