Sub topic 11
Finding out why something did or did not apply
This chapter assumes you know how auto-configuration is discovered through the AutoConfiguration.imports file. It also assumes you know the main @Conditional annotations, and that you can run a Spring Boot application from the command line. Here you will learn to answer the most common auto-configuration question: why is this bean present, or why is it missing?
Spring Boot records the outcome of every condition it evaluates. You can read that record as a log report, as JSON from an Actuator endpoint, or through the ConditionEvaluationReport class. You do not need to guess.
Turning the report on
The simplest way is the --debug flag, shown here with the packaged jar.
java -jar app.jar --debug--debug is the command-line form of the debug property. These are equivalent:
debug=trueThe flag does not set every logger to DEBUG. It turns on debug logging for a small set of core loggers (embedded container, Hibernate SQL generation, Spring Boot itself). One of them is the listener that prints the conditions evaluation report.
If the application context fails to start, Spring Boot prints the report automatically, as a short message telling you to re-run with debug enabled. In that case the report is the first place to look.
Reading the report
The report has four sections. This abridged excerpt shows the shape. The exact wording of the messages varies a little between Spring Boot versions.
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
GreetingAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.example.greeting.GreetingService' (OnClassCondition)
- @ConditionalOnProperty (greeting.enabled=true) matched (OnPropertyCondition)
Negative matches:
-----------------
GreetingAutoConfiguration#greetingService:
Did not match:
- @ConditionalOnMissingBean (types: com.example.greeting.GreetingService; SearchStrategy: all) found bean 'myGreetingService' (OnBeanCondition)
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'jakarta.jms.ConnectionFactory' (OnClassCondition)
Exclusions:
-----------
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Unconditional classes:
----------------------
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration| Section | Meaning |
|---|---|
| Positive matches | Every condition on the class or method passed, so its beans were registered. |
| Negative matches | At least one condition failed. The entry names the failing condition and the condition class that evaluated it. |
| Exclusions | Classes you removed with `exclude` or `spring.autoconfigure.exclude`. |
| Unconditional classes | Auto-configurations with no conditions. They always apply. |
Class entries and method entries
An entry that ends in #methodName, such as GreetingAutoConfiguration#greetingService, refers to a single @Bean method. An entry with no # refers to the whole class. This matters when you read the example above.
In that example the class GreetingAutoConfiguration matched, so it appears under Positive matches. Its bean method did not match, because the application defines its own GreetingService. Both facts are true, and both are shown. A class can be a positive match while some of its beans are negative matches. A negative match on the class means none of its beans were considered.
Why only one failed condition may be listed
Conditions on a class are checked in turn, and evaluation generally stops at the first one that fails. A negative match therefore tells you one reason the class was skipped. It does not list every reason. If you fix that condition and the class still does not apply, run the report again and read the new reason.
Where a class can be, and what it tells you
Search the report for the class name. There are five possible results.
| You find the class under | What it means | Next step |
|---|---|---|
| Positive matches | The class applied. | If a bean is still wrong, look at the method entries and at which bean wins. |
| Negative matches | A condition failed. | Read the failing condition and check its input: classpath, bean, or property. |
| Exclusions | Something removed it. | Look for `exclude` on `@SpringBootApplication` or the `spring.autoconfigure.exclude` property. |
| Unconditional classes | It always applies. | Look elsewhere for the cause of the problem. |
| Nowhere | It was never a candidate. | Check the imports file. |
A class that is nowhere in the report was never loaded as a candidate. For a custom auto-configuration, the usual causes are:
- The class name is not in
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. - The file is in the wrong directory, or has a typo in its name.
- The class name in the file is misspelled.
- The jar that contains the file is not on the application's classpath.
- The project still uses
spring.factoriesfor registration, which current Spring Boot versions no longer read for auto-configurations.
Diagnosing common failures
A `@ConditionalOnClass` mismatch
The message says a required class was not found. The dependency that provides it is missing from the build, or it is present only at test or provided scope. Add the dependency, or accept that the feature is off.
A `@ConditionalOnProperty` mismatch
The message shows the prefix, the name, and the expected value. Compare it to your application.properties. Common causes are a wrong prefix, a value other than the expected havingValue, or a profile-specific file that is not active. In the GreetingAutoConfiguration example, greeting.enabled=false fails the condition because the expected value is true.
A `@ConditionalOnBean` mismatch caused by order
This is the hardest case. The report says a bean of the required type was not found, yet you can see that your application has one. The cause is processing order. @ConditionalOnBean only sees bean definitions that have already been registered when it is evaluated. If the class that registers the bean is processed after the class with the condition, the condition fails.
Compare the order of the classes in the report with the order you expected. The fix is to declare the dependency on the other class, as in the AuditAutoConfiguration example:
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class AuditAutoConfiguration {
// beans omitted
}With after, the data source definition is registered before the condition runs.
A `@ConditionalOnMissingBean` back-off you did not expect
The report names the bean that caused the back-off, such as found bean 'myGreetingService'. Check whether that bean is a deliberate override or something that was registered by accident, for example by a component scan that picks up a class you did not mean to include.
Reading the same data from Actuator
When the application is already running, you do not need to restart it with --debug. The Actuator conditions endpoint returns the same information as JSON. Add the Actuator dependency, then expose the endpoint:
management.endpoints.web.exposure.include=conditionsThen query it. This example lists the names of all negative matches, using jq to read the JSON:
curl -s http://localhost:8080/actuator/conditions | jq '.contexts[].negativeMatches | keys'The response has one entry per application context. Inside each context it has positiveMatches, negativeMatches, and unconditionalClasses. Each match lists its conditions and a message, so you can filter and search it with ordinary JSON tools.
Practical tips
- Search for the class name first, not for the bean name. The report is organised by auto-configuration class and bean method.
- Redirect the output to a file when you read it. A typical report has several hundred entries.
- Find an entry for a class you know applies, and compare its wording with the one that fails. This helps you learn how each condition phrases its result.
- Change one thing at a time between runs. Because only the first failure is listed, a fix may only move you to the next one.
- If a bean applies but you still get the wrong behavior, the report cannot help much further. At that point check which bean is injected, using
@Primary, qualifiers, or a test that asserts on the bean type.
Exercise
You wrote a custom auto-configuration, ReportAutoConfiguration, and registered it in the imports file. Your application starts, but the ReportService bean is missing. You run with --debug and search the report for ReportAutoConfiguration. It is not in any of the four sections. What are two likely causes?
Show answer
The class was never a candidate. Likely causes are a missing or misspelled entry in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (or the file is in the wrong directory), or the jar that holds the file is not on the application's classpath. A failing condition would have placed the class under Negative matches, and an exclusion would have placed it under Exclusions.
Exercise
The report shows AuditAutoConfiguration under Negative matches with @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans of type javax.sql.DataSource. Your application has a working DataSource. What do you check, and what change might fix it?
Show answer
Check the processing order. @ConditionalOnBean only sees bean definitions already registered when it is evaluated, so AuditAutoConfiguration may be processed before the class that registers the DataSource. Add after = DataSourceAutoConfiguration.class to its @AutoConfiguration annotation, and make sure the condition is on an auto-configuration class, not on a regular user configuration class.
Exercise
In the report, GreetingAutoConfiguration is a positive match, but GreetingAutoConfiguration#greetingService is a negative match. Is this a contradiction? What does it tell you?
Show answer
It is not a contradiction. The class-level conditions passed, so the class was processed. The conditions on the greetingService method failed. In the earlier example this was @ConditionalOnMissingBean, because the application defined its own GreetingService. The negative entry names the bean that caused the back-off.