Scrivio Interview library
InternalsFoundations5 min read · updated 12 November 2026

Explain how @SpringBootApplication leads to a DataSource bean being created when you add spring-boot-starter-jdbc and a database driver.

@SpringBootApplication includes @EnableAutoConfiguration, which imports AutoConfigurationImportSelector, a DeferredImportSelector that runs after our own configuration has been processed. It reads the candidate classes listed in every META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file on the classpath, one of which is DataSourceAutoConfiguration. That class's conditions, such as @ConditionalOnClass for DataSource and the embedded database types, pass because the JDBC starter and the driver are on the classpath, and its DataSource bean is created only if we have not defined one, through @ConditionalOnMissingBean.

Why this question is asked

Adding two dependencies and getting a working connection pool feels like magic, and interviewers want to see that we can explain it as ordinary Spring mechanics. Knowing the chain also tells us where to look when it fails: a missing condition, a missing file entry or a bean of our own. In this answer, we will unpack @SpringBootApplication, follow AutoConfigurationImportSelector as it finds candidates, see the conditions on DataSourceAutoConfiguration pass one by one, and end with the back-off check that decides whether the bean is created.

1What @SpringBootApplication Is Made Of

@SpringBootApplication is a shortcut for three annotations. @SpringBootConfiguration marks the class as a configuration class (it is a specialised @Configuration). @ComponentScan scans the package of the class and its subpackages for our @Component, @Service and @Configuration classes. @EnableAutoConfiguration switches on auto-configuration, and that is the one this question is about.

Java
@SpringBootApplication // = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}

@EnableAutoConfiguration itself is small. Its important line is @Import(AutoConfigurationImportSelector.class), which hands the job of finding auto-configurations to that selector.

2AutoConfigurationImportSelector and Why It Is Deferred

An import selector is a class that Spring asks, while it processes configuration, which further configuration classes to import. AutoConfigurationImportSelector is a DeferredImportSelector, a special kind that Spring calls only after it has processed all the regular configuration classes, including everything component scanning found.

The reason is the back-off rule we will meet at the end. Auto-configurations ask whether we already defined a bean, and they can only answer that correctly if our bean definitions are already registered. Deferring the import guarantees that our configuration is complete before any auto-configuration is evaluated.

Step throughFrom @SpringBootApplication to a DataSource
ContextImport selectorImports filesDataSourceAutoConfig
process user config and component scan
deferred: which auto-configurations?
read AutoConfiguration.imports on classpath
about 150+ candidate class names
filter by metadata, sort
remaining candidates
evaluate conditions
register DataSource bean

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

3Finding the Candidates

Every Boot jar that offers auto-configuration contains a plain text file at META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, with one fully qualified class name per line. The selector loads all such files from the classpath through ImportCandidates.

Text
# inside the Boot auto-configuration jar (excerpt)
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
...

In Boot 3 these live in spring-boot-autoconfigure. Boot 4 split auto-configuration into smaller modules, so DataSourceAutoConfiguration moved into the JDBC module with a new package name (org.springframework.boot.jdbc.autoconfigure in Boot 4; check your version). The mechanism is unchanged: each module ships its own imports file.

Before full evaluation, the selector drops candidates that obviously cannot apply, using precomputed metadata so it does not have to load every class, and it removes anything listed in spring.autoconfigure.exclude. Then it sorts the rest by their declared before and after ordering.

4Why the DataSource Conditions Pass

DataSourceAutoConfiguration is guarded by conditions that describe when a DataSource makes sense. At the class level, @ConditionalOnClass requires javax.sql.DataSource and Spring's EmbeddedDatabaseType to be present. spring-boot-starter-jdbc brings spring-jdbc, which contains EmbeddedDatabaseType, and the JDK provides DataSource, so the class passes. The starter also brings HikariCP, which matters for the next step.

Java
// simplified shape of the conditions
@AutoConfiguration(before = SqlInitializationAutoConfiguration.class)
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) // spring-jdbc on the classpath: pass
@EnableConfigurationProperties(DataSourceProperties.class) // binds spring.datasource.*
public class DataSourceAutoConfiguration {
@Configuration
@Conditional(PooledDataSourceCondition.class) // a pool such as Hikari is present: pass
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) // we defined none: pass
@Import({ DataSourceConfiguration.Hikari.class, ... })
static class PooledDataSourceConfiguration { }
}
// DataSourceConfiguration.Hikari: @ConditionalOnClass(HikariDataSource.class) -> creates HikariDataSource
// from spring.datasource.url=jdbc:postgresql://db:5432/shop and the PostgreSQL driver on the classpath

The driver jar plays its part at runtime. Boot reads spring.datasource.url and, from the jdbc:postgresql prefix, picks org.postgresql.Driver, which must be on the classpath. If no URL is set and an embedded database such as H2 is on the classpath, a different branch, EmbeddedDatabaseConfiguration, creates an in-memory database instead.

5The Final Check: Did We Define Our Own?

The last gate is @ConditionalOnMissingBean on DataSource. Because the selector was deferred, all our bean definitions are registered by now. If we declared a DataSource @Bean, the condition fails and Boot's pool is not created; ours is used instead. If we did not, the condition passes and Boot registers a HikariDataSource configured from spring.datasource.* and spring.datasource.hikari.*.

We can see every one of these decisions in the condition evaluation report, printed with debug=true, where DataSourceAutoConfiguration appears under positive matches with the reasons: found required classes, and did not find any beans of type DataSource.

6Follow-up questions

Why are auto-configurations processed after user configuration?

So that @ConditionalOnMissingBean and @ConditionalOnBean can see our bean definitions. If auto-configurations ran first, Boot would create its DataSource before ours was registered, and we would end up with two beans or with Boot's default overriding our intent. Deferring them makes user configuration always win.

What happens if I add the JDBC starter but no driver and no URL?

With no embedded database on the classpath and no URL, Boot cannot decide what to connect to, so startup fails with an error explaining that it failed to configure a DataSource because no URL was specified and no embedded database could be configured. A failure analyzer prints that message with suggestions.

How do you turn this auto-configuration off?

List it in spring.autoconfigure.exclude or in @SpringBootApplication(exclude = DataSourceAutoConfiguration.class). The class is then removed from the candidates before evaluation.

7Conclusion

In this answer, we followed the chain: @SpringBootApplication includes @EnableAutoConfiguration, which imports the deferred AutoConfigurationImportSelector; the selector reads AutoConfiguration.imports files to find DataSourceAutoConfiguration; its class conditions pass because the starter brought spring-jdbc and HikariCP and the driver handles the URL; and @ConditionalOnMissingBean creates the DataSource only when we have not defined one.

What to say in the interviewAbout 70 seconds aloud

Open with

@SpringBootApplication combines three annotations: @SpringBootConfiguration, @ComponentScan and @EnableAutoConfiguration. The last one imports AutoConfigurationImportSelector, which is a DeferredImportSelector, so Spring calls it only after it has processed all my configuration and component scanning.

Then, if they want more

The selector reads every META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file on the classpath; Boot's own file lists DataSourceAutoConfiguration among a long list of candidates. It filters out candidates that obviously can't apply using precomputed metadata, removes exclusions, and sorts the rest. Then each candidate's conditions are evaluated. DataSourceAutoConfiguration has @ConditionalOnClass for DataSource and EmbeddedDatabaseType, and those pass because spring-boot-starter-jdbc brought spring-jdbc.

Its pooled configuration needs a pool class, and the starter brings HikariCP, so the Hikari configuration applies. It binds spring.datasource properties, and the PostgreSQL driver on the classpath handles the jdbc:postgresql URL.

The last gate is @ConditionalOnMissingBean on DataSource: because the selector was deferred, my beans are already registered, so if I defined my own DataSource, Boot backs off; otherwise it registers a HikariDataSource. I can confirm all of that in the condition evaluation report with debug=true.

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 "Explain how @SpringBootApplication leads to a DataSource bean being created when you add...": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/c5461505be/

More in Auto-configuration