Scrivio Interview library
DebuggingIntermediate5 min read · updated 12 November 2026

A custom auto-configuration uses @ConditionalOnBean(DataSource.class) but the bean is never created even though the application has a DataSource. What is the likely cause?

@ConditionalOnBean only sees bean definitions that have been registered so far, so its result depends on processing order. If the custom auto-configuration is evaluated before DataSourceAutoConfiguration, the DataSource is not defined yet, the condition fails, and the bean is never created, even though a DataSource exists a moment later. The fix is @AutoConfiguration(after = DataSourceAutoConfiguration.class); the condition evaluation report shows the failed condition and its reason, and the same timing problem is why @ConditionalOnBean is unreliable in regular @Configuration classes.

Why this question is asked

This bug is confusing because both halves of it look correct. The application clearly has a DataSource, the Actuator beans endpoint shows it, and the condition clearly asks for one. The problem is not what the condition checks but when it checks it. Interviewers like this question because it tests whether we understand that bean conditions run during registration, not after the context is built. In this answer, we will recap when bean conditions are evaluated, reproduce the failure, read the report, apply the fix, and explain why the same condition is risky in ordinary configuration classes.

1A Quick Recap: When Bean Conditions Are Evaluated

Spring builds an application context in two broad phases. First it registers bean definitions, descriptions of what to create, by processing configuration classes one after another. Then it creates the beans. Conditions are evaluated in the first phase, while a configuration class is being processed, to decide whether its bean definitions should be registered at all.

So @ConditionalOnBean(DataSource.class) does not ask whether the finished application will have a DataSource. It asks whether a DataSource bean definition has been registered by the time this class is processed. For auto-configurations, the processing order is decided by Boot's sorter, which starts alphabetically and then applies ordering declarations.

2Reproducing the Failure

Here is a custom auto-configuration without any ordering declaration.

Java
@AutoConfiguration
@ConditionalOnBean(DataSource.class)
public class AuditJdbcAutoConfiguration {
@Bean
AuditJdbcWriter auditJdbcWriter(DataSource ds) { return new AuditJdbcWriter(ds); }
}
// at startup:
// AuditJdbcAutoConfiguration evaluated -> no DataSource definition yet -> skipped
// DataSourceAutoConfiguration evaluated -> HikariDataSource registered
// result: DataSource exists, AuditJdbcWriter does not
Step throughEvaluated too early
SorterAuditJdbcAutoConfigDataSourceAutoConfigBean registry
process first (no ordering declared)
any DataSource definition?
none yet
condition fails: skip AuditJdbcWriter
process next
register HikariDataSource

6 messages in order. Play them, or step through them one at a time.

The DataSource arrives one step too late for the condition to see it.

Without before or after declarations, the relative order of the two classes is effectively accidental. Here the custom class happened to be processed first. Another service, with a different set of auto-configurations, might process them the other way around and work, which is why such bugs often appear only in some services.

3Reading the Condition Evaluation Report

Starting the application with debug=true, or calling Actuator's conditions endpoint, prints the condition evaluation report. Our class appears under negative matches, and the message tells us exactly what the condition saw.

Java
============================
CONDITIONS EVALUATION REPORT
============================
Negative matches:
-----------------
AuditJdbcAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans of type javax.sql.DataSource (OnBeanCondition)

Seeing did not find any beans of type DataSource, while knowing that the application has one, is the signature of an ordering problem. The report states the time-of-check fact, and our job is to make the check happen later.

4The Fix: Declare the Order

We tell Boot's sorter that our auto-configuration must be processed after the one that registers the DataSource.

Java
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class AuditJdbcAutoConfiguration { ... }
// DataSourceAutoConfiguration evaluated -> HikariDataSource registered
// AuditJdbcAutoConfiguration evaluated -> DataSource found -> AuditJdbcWriter registered
// report: @ConditionalOnBean (types: javax.sql.DataSource) found bean 'dataSource'

The after attribute always wins over alphabetical and numeric order, so the result no longer depends on the other auto-configurations in the service. Many teams add a second condition that makes the intent stricter, @ConditionalOnSingleCandidate(DataSource.class), so the writer is created only when there is one DataSource to use. On Boot 4, DataSourceAutoConfiguration lives in a new module and package, so the import changes; check your version.

5Why @ConditionalOnBean Is Unreliable in Regular @Configuration

In the application's own configuration classes, there is no sorter and no before or after. Their processing order depends on component scanning and on which class imports which, and auto-configurations, which define most infrastructure beans, are processed after all of them. So a @ConditionalOnBean(DataSource.class) in a user @Configuration class almost always runs before Boot's DataSource exists and fails even when everything is set up correctly.

That is why Boot's documentation says bean conditions are intended for auto-configuration classes. In application code, the simpler tools work better: inject ObjectProvider<DataSource> and act only if a bean is available, or use @ConditionalOnProperty to make the feature an explicit choice.

Where bean conditions are reliable
Where the condition isOrder controlled byReliable?
Auto-configuration with after = ...AutoConfigurationSorteryes
Auto-configuration without orderingalphabetical accidentsometimes
Regular @Configuration classscan and import order, before auto-configno

6Follow-up questions

What is @ConditionalOnSingleCandidate for?

It matches when exactly one bean of the type exists, or when several exist and one of them is marked @Primary. Boot uses it for beans that need a single obvious dependency, such as JdbcTemplate needing one DataSource. It avoids creating a bean that would fail with an ambiguous injection when an application has two DataSources and no primary.

Would @ConditionalOnMissingBean have the same problem?

Yes, in the opposite direction. If our auto-configuration runs before the one that defines a competing bean, it registers its default first, and the later one sees ours and backs off, or both end up registered. Ordering declarations protect both kinds of bean conditions.

Why not just remove the condition and inject DataSource?

Then the auto-configuration would fail in services without a DataSource. The condition exists so the library can be added to any service and only activates where it can work.

7Conclusion

In this answer, we saw that @ConditionalOnBean checks bean definitions registered so far, so an auto-configuration processed before DataSourceAutoConfiguration does not see the DataSource. The report shows did not find any beans, @AutoConfiguration(after = DataSourceAutoConfiguration.class) fixes it, @ConditionalOnSingleCandidate makes it stricter, and in regular configuration classes bean conditions are unreliable because auto-configuration runs after them.

What to say in the interviewAbout 80 seconds aloud

Open with

The likely cause is ordering. @ConditionalOnBean is evaluated while bean definitions are being registered, and it only sees definitions registered so far, not the final context. If our auto-configuration is processed before DataSourceAutoConfiguration, there's no DataSource definition yet, the condition fails and our bean is skipped, even though a DataSource exists a moment later.

Then, if they want more

Without ordering declarations, Boot sorts auto-configurations alphabetically, so whether it works can even differ between services. I'd confirm it with the condition evaluation report, using debug=true or the Actuator conditions endpoint: our class shows under negative matches with did not find any beans of type DataSource, which, when we know a DataSource exists, means the check ran too early.

The fix is @AutoConfiguration with after equals DataSourceAutoConfiguration.class, which always wins over alphabetical order, so the DataSource is registered first. I'd often use @ConditionalOnSingleCandidate as well, which matches when there's exactly one DataSource or one marked @Primary, so we don't create a bean that would fail on an ambiguous injection.

And I'd point out that @ConditionalOnBean in a regular @Configuration class is unreliable for the same reason, worse actually, because user configuration is processed before all auto-configurations. In application code I'd use ObjectProvider or a property instead.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A custom auto-configuration uses @ConditionalOnBean(DataSource.class) but the bean is...": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/db61566fc5/